In react-admin get access to redux store

后端 未结 2 2021
长发绾君心
长发绾君心 2021-01-04 12:13

My question is related to react-admin repo.

I want to dispatch an action, outside of scope of a component, in order to do that, I\'ve read that I need to get access

2条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-01-04 12:58

    When you say that you need to dispatch an action outside the scope of a component, I suppose that it's in reaction to another action that was dispatched in the past.

    In that case, that's what react-admin calls a side effect. React-admin handles side effects using redux-saga. Here is how to create a custom saga:

    // in src/bitcoinSaga.js
    import { put, takeEvery } from 'redux-saga/effects';
    import { showNotification } from 'react-admin';
    
    export default function* bitcoinSaga() {
        yield takeEvery('BITCOIN_RATE_RECEIVED', function* () {
            yield put(showNotification('Bitcoin rate updated'));
        })
    }
    

    Register this saga in the component as follows:

    // in src/App.js
    import React from 'react';
    import { Admin } from 'react-admin';
    
    import bitcoinSaga from './bitcoinSaga';
    
    const App = () => (
        
            ...
        
    );
    
    export default App;
    

    This is documented in the react-admin documentation, in the chapter.

提交回复
热议问题