我试图将商店实例(商店状态)放在反应组件之外,即在单独的帮助器函数中.我有我的减速机,我的动作,我在最上面的组件中创建了一个商店.
// 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());
}Run Code Online (Sandbox Code Playgroud)
这种方法不起作用,因为console.log(store.getState())返回undefined.但是,如果我将initialConfig传递给configStore(),它会构建一个新的商店,一切正常.
感谢帮助.