admin管理员组文章数量:1336304
I'm trying to get the store instance (store state) outside a react ponent, namely in a separate helper function. I have my reducer, my action, I have created a store in the most upper ponent.
// 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());
}
I'm trying to get the store instance (store state) outside a react ponent, namely in a separate helper function. I have my reducer, my action, I have created a store in the most upper ponent.
// 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());
}
This approach is not working as console.log(store.getState()) returns undefined. However, if I pass an initialConfig to configStore() it builds a new store and everything works just fine.
Thanks for help.
Share Improve this question edited Jul 11, 2018 at 22:27 Damjan Pavlica 34.1k10 gold badges75 silver badges78 bronze badges asked Oct 26, 2017 at 10:08 dalliondallion 1955 silver badges16 bronze badges 3- You can move store initialization code into separate module, export store and import it whereever you need to use it. – Flying Commented Oct 26, 2017 at 10:11
- Could you be more specific please? I'm creating the store in a separate file, I'm calling the configStore() function in index.js – dallion Commented Oct 26, 2017 at 10:13
- Take a look at my answer below for example solution – Flying Commented Oct 26, 2017 at 10:17
1 Answer
Reset to default 6Your current code is not working because you're creating separate stores into index.js
and helpers.js
while you should use same Redux store.
You can move store initialization code into separate module, export store and import it whereever you need to use it.
// 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());
}
本文标签: javascriptReact Reduxaccessing existing store in a helper functionStack Overflow
版权声明:本文标题:javascript - React Redux - accessing existing store in a helper function - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742398571a2467421.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论