In React-admin Get Access To Redux Store
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 to the actual re
Solution 1:
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.jsimport { put, takeEvery } from'redux-saga/effects';
import { showNotification } from'react-admin';
exportdefaultfunction* bitcoinSaga() {
yieldtakeEvery('BITCOIN_RATE_RECEIVED', function* () {
yieldput(showNotification('Bitcoin rate updated'));
})
}
Register this saga in the <Admin>
component as follows:
// in src/App.jsimportReactfrom'react';
import { Admin } from'react-admin';
import bitcoinSaga from'./bitcoinSaga';
constApp = () => (
<AdmincustomSagas={[bitcoinSaga ]} dataProvider={simpleRestProvider('http://path.to.my.api')}>
...
</Admin>
);
exportdefaultApp;
This is documented in the react-admin documentation, in the <Admin>
chapter.
Solution 2:
You could also simply use custom reducers if the computation is no async
// in src/App.jsimportReactfrom'react';
import { Admin } from'react-admin';
import reducers from'./reducers';
constApp = () => (
<AdmincustomReducers={customReducers}dataProvider={simpleRestProvider('http://path.to.my.api')}>
...
</Admin>
);
exportdefaultApp;
Post a Comment for "In React-admin Get Access To Redux Store"