Error: Expected the root reducer to be a function. Instead, received: 'undefined'

Error: Expected the root reducer to be a function. Instead, received: 'undefined'

我一直运行进入这个错误!

错误:预期根减速器是一个函数。相反,收到:'undefined'

我已经尝试了我能找到的所有答案,但都无济于事,这里是所有相关内容!

根减速器

const createRootReducer = (history) => {
      combineReducers({
        router: connectRouter(history),
        createUser: signupReducer,
      });
    };

    export default createRootReducer;

根目录

const Root = ({ children, initialState = {} }) => {
  const history = createBrowserHistory();
  const middleware = [thunk, routerMiddleware(history)];

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

  return (
    <Provider store={store}>
      <ConnectedRouter history={history}>{ children }</ConnectedRouter>
    </Provider>
  );
};

export default Root;

应用

function App() {
  return (
    <div className="App">
      <Root>
        <ToastContainer hideProgressBar={true} newestOnTop={true} />
        <Navbar />
        <Landing />
        <PostList />
      </Root>
    </div>
  );
}

export default App;

注册减速器

export const signupReducer = (state = initialState, action) => {
  switch (action.type) {
    case UserTypes.CREATE_USER_SUBMITTED:
      return {
        usernameError: "",
        passwordError: "",
        isSubmitted: true
      };
    case UserTypes.CREATE_USER_SUCCESS:
      return {
        usernameError: "",
        passwordError: "",
        isSubmitted: false
      };
    case UserTypes.CREATE_USER_ERROR:
        const err = {
        usernameError: "",
        passwordError: "",
        isSubmitted: false
      };
      if(action.errorData.hasOwnProperty('username')){
        err.usernameError = action.errorData['username'];
      }
      if(action.errorData.hasOwnProperty('password')){
        err.passwordError = action.errorData['password'];
      }
      return err;
    default:
      return state
  }
}

这是在根组件中抛出错误的行

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

感谢您的宝贵时间!

你需要return组合减速器。

const createRootReducer = (history) => {
  return combineReducers({
    router: connectRouter(history),
    createUser: signupReducer,
  });
};

或使用箭头函数隐式return。

const createRootReducer = (history) => combineReducers({
  router: connectRouter(history),
  createUser: signupReducer,
});
  • 当您最初在项目中设置 redux 并且您还没有创建任何 reducer 时会出现此错误。
  • 所以你必须用 Dummy Reducers 来欺骗 Redux。
    例如:

export default combineReducers({ 
  test: () => 'test reducer' 
});

  • 在此处创建减速器绑定后
    例如:

import { combineReducers } from 'redux'; 
import productReducer from './productReducer'

export default combineReducers({ 
  products: productReducer 
})