Skip to content Skip to sidebar Skip to footer

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.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 <Admin> component as follows:

// in src/App.js
import React from 'react';
import { Admin } from 'react-admin';

import bitcoinSaga from './bitcoinSaga';

const App = () => (
    <Admin customSagas={[ bitcoinSaga ]} dataProvider={simpleRestProvider('http://path.to.my.api')}>
        ...
    </Admin>
);

export default App;

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.js

    import React from 'react';
    import { Admin } from 'react-admin';

    import reducers from './reducers';

    const App = () => (
        <Admin customReducers={customReducers} dataProvider={simpleRestProvider('http://path.to.my.api')}>
            ...
        </Admin>
    );

    export default App;


Post a Comment for "In React-admin Get Access To Redux Store"