错误:期望reducer成为一个函数

时间:2018-02-24 02:29:20

标签: reactjs redux react-redux

我收到错误“期望减速器成为一种功能。”。

我正在尝试在使用create-react-app创建的简单反应项目中添加redux.I创建减速器但也会出现错误“预期减速器将成为一个函数”。我的代码是

    import React from 'react';
    import ReactDOM from 'react-dom';
    import './index.css';
    import App from './App';
    import registerServiceWorker from './registerServiceWorker';
    import {combineReducers, createStore} from 'redux';

    function productReducer(state=[],action){
      return state;
    }
    function userReducer(state='',action){
      return state;
    }
    const store = createStore(allReducers);

     console.log(store.getState());

     const allReducers  = store.combineReducers({
       products :productReducer,
       user :userReducer
     });


    console.log(store.getState());
    ReactDOM.render(<App />, document.getElementById('root'));
    registerServiceWorker();

1 个答案:

答案 0 :(得分:2)

combineReducer不需要在商店对象上调用。

import React from 'react';
import ReactDOM from 'react-dom';
import './index.css';
import App from './App';
import registerServiceWorker from './registerServiceWorker';
import {combineReducers, createStore} from 'redux';

function productReducer(state=[],action){
  return state;
}
function userReducer(state='',action){
  return state;
}

 const allReducers  = combineReducers({
   products :productReducer,
   user :userReducer
 });

const store = createStore(allReducers);

console.log(store.getState());
ReactDOM.render(<App />, document.getElementById('root'));
registerServiceWorker();