当我更新redux状态时,它给出错误TypeError:对象是不可迭代的(无法读取属性Symbol(Symbol.iterator)属性]
const initialState = {
articles: []
};
function rootReducer(state = initialState, action) {
switch(action.type){
case ADD_ARTICLE:{return [...state,action.payload];} # error occurs in this line
default:return state;
}
}
export default rootReducer;
您正在尝试克隆{}
而不是[]
的状态。尝试返回以下内容:
switch(action.type) {
case ADD_ARTICLE: {
return {
...state,
action.payload
};
}
default: {
return ...state;
}
}