在 react-admin 中访问 redux store [英] In react-admin get access to redux store
问题描述
我的问题与 react-admin 存储库有关.
My question is related to react-admin repo.
我想在组件范围之外分派一个动作,以便做到这一点,我读过 我需要访问实际的 redux 存储本身,并直接分派,
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,
所以我知道 Admin
组件有一个 initialState
属性,但它只接受默认状态对象,而不是商店.所以我不能创建一个 store 并传入.
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.
我的问题是:
- 如何访问
Admin
组件的 redux 存储? - 使用 Admin 作为我的主要应用组件时,如何在组件外分派操作?
- 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>
推荐答案
当你说你需要在组件范围之外分派一个动作时,我想它是对过去分派的另一个动作的反应.
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.
在那种情况下,这就是 react-admin 所说的副作用.React-admin 使用 redux-saga 处理副作用.以下是创建自定义传奇的方法:
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'));
})
}
在
组件中注册这个saga如下:
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;
这在 react-admin 文档中有记录,在 <;管理员>
章节.
This is documented in the react-admin documentation, in the <Admin>
chapter.
这篇关于在 react-admin 中访问 redux store的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!