我一直遇到这个错误!
错误:根减少器应为函数。相反,收到:"未定义">
我已经试过了所有我能找到的答案,但都无济于事,以下是所有相关的东西!
根部还原剂
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));
感谢您抽出时间!
您需要返回组合的减速器。
const createRootReducer = (history) => {
return combineReducers({
router: connectRouter(history),
createUser: signupReducer,
});
};
或者使用箭头函数隐式返回。
const createRootReducer = (history) => combineReducers({
router: connectRouter(history),
createUser: signupReducer,
});
- 当您最初在项目中设置redux,但尚未创建任何reducer时,就会出现此错误
- 所以你必须用假Reducers欺骗Redux
例如:
export default combineReducers({
test: () => 'test reducer'
});
- 在此处创建reducer绑定后
,例如:
import { combineReducers } from 'redux';
import productReducer from './productReducer'
export default combineReducers({
products: productReducer
})