无法访问非反应组件上的商店

时间:2017-04-21 15:38:46

标签: reactjs redux

redux getState方法不可用。我是否需要任何中间件来实现所需的结果。以下是代码

const configureStore = (initialState = {}) => {
  return createStore(
    rootReducer,
    initialState,
    applyMiddleware(...middleware),
  );
};

export default configureStore;

util.js

import store  from '../../store';

store.getState()

// _store2.default.getState is not a function

2 个答案:

答案 0 :(得分:3)

您正在返回一个功能而不是您的商店。好像你和商店混合了减压器。你想要的是这个:

const store = createStore(
    rootReducer,
    initialState,
    applyMiddleware(...middleware),
);


export default store;

答案 1 :(得分:0)

看起来您打算在某个时候初始化商店。

我不确定您要如何布局应用,但

// index.js

import configureStore from 'store';

export const store = configureStore({ ... someInitialState });

然后

// util.js

import store from 'index';

// use store

我不认为这是最好的解决方案 - 而不是从store导出商店初始化程序,只需初始化商店并导出商店。

相关问题