问题描述
我的问题与 repo有关。
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
prop,但它只接受默认状态对象,而不是商店。所以我不能建立商店并将其传入。
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使用处理副作用。以下是如何在src / bitcoinSaga.js $ b中创建自定义传奇:
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'));
})
}
在中注册此传奇< Admin>
组件如下:
// 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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!