小编Dav*_*idN的帖子

React Redux - 在辅助函数中访问现有商店

我试图将商店实例(商店状态)放在反应组件之外,即在单独的帮助器函数中.我有我的减速机,我的动作,我在最上面的组件中创建了一个商店.

// 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(),它会构建一个新的商店,一切正常.

感谢帮助.

javascript store reactjs react-redux

5
推荐指数
1
解决办法
3462
查看次数

标签 统计

javascript ×1

react-redux ×1

reactjs ×1

store ×1