Error: Reducer “good” returned undefined during initialization
Error: Reducer "good" returned undefined during initialization. If the state passed to the reducer is undefined, you must explicitly return the initial state. The initial state may not be undefined. If you don't want to set a value for this reducer, you can use null instead of undefined.
原因:定义reducer时,设置的匹配失败时的返回state的值有问题
解决方案:
这是之前的代码:
export default function goodReducer(state, action) {
switch (action.type) {
case "add":
// 添加数据
return state;
default:
// 什么也不做,匹配失败
return state;
}
}
这是修改后的代码:
export default function goodReducer(state, action) {
switch (action.type) {
case "add":
// 添加数据
return state;
default:
// 什么也不做,匹配失败
return {...state};
}
}