React Redux - 在辅助函数中访问现有商店
React Redux - accessing existing store in a helper function
我试图在 React 组件之外获取商店实例(商店状态),即在单独的辅助函数中。我有我的 reducer,我的 action,我在最上层的组件中创建了一个 store。
// configStore.js
import { createStore } from 'redux';
import generalReducers from '../reducers/generalReducers';
export default function configStore(initialState) {
return createStore(generalReducers, initialState);
}
// index.js
import { Provider } from 'react-redux';
import configStore from './store/configStore';
const initialReduxStoreConfig = {
unit: 'm2',
language: 'en'
}
const store = configStore(initialReduxStoreConfig);
ReactDOM.render((
<Provider store={store}>
<App/>
</Provider>
), document.querySelector('#root'));
// helpers.js
import configStore from '../store/configStore';
const store = configStore();
function getTranslation(key, lang = null) {
console.log(store.getState());
}
此方法无效,因为 console.log(store.getState()) returns 未定义。但是,如果我将 initialConfig 传递给 configStore(),它会建立一个新商店,一切正常。
感谢您的帮助。
您当前的代码无法正常工作,因为您正在为 index.js
和 helpers.js
创建单独的商店,而您应该使用相同的 Redux 商店。
您可以将商店初始化代码移动到单独的模块中,导出商店并在需要使用它的任何地方导入它。
// configStore.js
import {createStore} from 'redux';
import generalReducers from '../reducers/generalReducers';
export default function configStore(initialState) {
return createStore(generalReducers, initialState);
}
// store.js
import configStore from './store/configStore';
const initialReduxStoreConfig = {
unit: 'm2',
language: 'en'
}
const store = configStore(initialReduxStoreConfig);
export default store;
// index.js
import {Provider} from 'react-redux';
import store from './store';
ReactDOM.render((
<Provider store={store}>
<App/>
</Provider>
), document.querySelector('#root'));
// helpers.js
import store from './store';
function getTranslation(key, lang = null) {
console.log(store.getState());
}
我试图在 React 组件之外获取商店实例(商店状态),即在单独的辅助函数中。我有我的 reducer,我的 action,我在最上层的组件中创建了一个 store。
// configStore.js
import { createStore } from 'redux';
import generalReducers from '../reducers/generalReducers';
export default function configStore(initialState) {
return createStore(generalReducers, initialState);
}
// index.js
import { Provider } from 'react-redux';
import configStore from './store/configStore';
const initialReduxStoreConfig = {
unit: 'm2',
language: 'en'
}
const store = configStore(initialReduxStoreConfig);
ReactDOM.render((
<Provider store={store}>
<App/>
</Provider>
), document.querySelector('#root'));
// helpers.js
import configStore from '../store/configStore';
const store = configStore();
function getTranslation(key, lang = null) {
console.log(store.getState());
}
此方法无效,因为 console.log(store.getState()) returns 未定义。但是,如果我将 initialConfig 传递给 configStore(),它会建立一个新商店,一切正常。
感谢您的帮助。
您当前的代码无法正常工作,因为您正在为 index.js
和 helpers.js
创建单独的商店,而您应该使用相同的 Redux 商店。
您可以将商店初始化代码移动到单独的模块中,导出商店并在需要使用它的任何地方导入它。
// configStore.js
import {createStore} from 'redux';
import generalReducers from '../reducers/generalReducers';
export default function configStore(initialState) {
return createStore(generalReducers, initialState);
}
// store.js
import configStore from './store/configStore';
const initialReduxStoreConfig = {
unit: 'm2',
language: 'en'
}
const store = configStore(initialReduxStoreConfig);
export default store;
// index.js
import {Provider} from 'react-redux';
import store from './store';
ReactDOM.render((
<Provider store={store}>
<App/>
</Provider>
), document.querySelector('#root'));
// helpers.js
import store from './store';
function getTranslation(key, lang = null) {
console.log(store.getState());
}