当reducer为空时,Store没有有效的reducer

时间:2017-10-21 07:20:09

标签: javascript reactjs react-redux

我正在使用https://github.com/stylesuxx/generator-react-webpack-redux中的生成器。除了我在浏览器控制台中出错

外,开发进展顺利

warning.js:10 Store does not have a valid reducer. Make sure the argument passed to combineReducers is an object whose values are reducers.

index.js in reduce文件中,代码如下所示:

import { combineReducers } from 'redux';

const reducers = {};
const combined = combineReducers(reducers);
module.exports = combined;

在商店文件的index.js中,代码如下所示:

import { createStore } from 'redux';
import reducers from '../reducers';

function reduxStore(initialState) {
  const store = createStore(reducers, initialState,
    window.devToolsExtension && window.devToolsExtension());

  if (module.hot) {
    // Enable Webpack hot module replacement for reducers
    module.hot.accept('../reducers', () => {
      // We need to require for hot reloading to work properly.
      const nextReducer = require('../reducers');  // eslint-disable-line global-require

      store.replaceReducer(nextReducer);
    });
  }

  return store;
}

export default reduxStore;

我目前没有在当前开发中使用redux,但我使用生成器进行配置以供将来规划。任何想法如何使减速器像const reducers = {};上面的代码一样空,而不会触发任何警告?

2 个答案:

答案 0 :(得分:1)

您需要的是错误建议的有效减速器。 如果您想保留样板或

,可以尝试以下操作
const reducers = { somename: () => {} };
const combined = combineReducers(reducers);
module.exports = combined;

或尝试

module.exports = () => {};

这将确保返回有效的reducer函数。因为你没有使用redux,所以这不是问题。

答案 1 :(得分:0)

为什么不尝试创建函数而不是空对象?

根据redux文档,它应该是一个函数:

  

reducer(Function):一个reduce函数,在给定当前状态树和要处理的动作的情况下返回下一个状态树。   https://github.com/reactjs/redux/blob/master/docs/api/createStore.md

此外,我不知道您是否需要使用combinereducers,因为您只有一个。{/ p>