Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 redux store itself, and dispatch on in directly,

so I know that the Admin component has an initialState prop, but it only accepts default state object, not the store. So I can't make a store and pass it in.

My question is:

  • How do I access redux store of an Admin component?
  • How can I dispatch an action outside of a component, when using Admin as my main app component?

my current app entry looks like this:

<AppLayoutDirection>
    <Admin
      title="My App"
      locale="en"
      dataProvider={dataProvider}
      authProvider={authProvider}
      i18nProvider={i18nProvider}
      theme={themeProvider}
      customSagas={customSagas}
      appLayout={AppLayout}
    >
      {DynamicResource}
    </Admin>
  </AppLayoutDirection>
like image 920
Developerium Avatar asked Feb 07 '19 09:02

Developerium


People also ask

How do I access Redux store in React?

It's simple to get access to the store inside a React component – no need to pass the store as a prop or import it, just use the connect function from React Redux, and supply a mapStateToProps function that pulls out the data you need. Then, inside the component, you can pass that data to a function that needs it.

Does react Admin use Redux?

The most important difference between react-admin v3 and v4 lies under the hood. React-admin v4 no longer uses Redux for state management - without changing the developer API.

How do I access the store in React component?

Understanding Context Usage​ createContext() , called ReactReduxContext . React Redux's <Provider> component uses <ReactReduxContext. Provider> to put the Redux store and the current store state into context, and connect uses <ReactReduxContext. Consumer> to read those values and handle updates.


1 Answers

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.

like image 160
François Zaninotto Avatar answered Sep 28 '22 07:09

François Zaninotto