【发布时间】:2021-10-09 23:25:27
【问题描述】:
我正在研究我的减速器,我正在努力让 UPDATE 工作。
当某个对象 ID 在状态发生变化时,我只是想更新我的反应前端上的列表。我怎样才能让它在这个reducer的前端改变具有特定ID的项目。
我在 UPDATE_ANIMALS 中尝试过,但没有成功
case 'UPDATE_ANIMALS':
return [
...state, item => item.id === action.payload.id ? action.payload : item
];
下面是我的整个减速器。
export const ANIMALSReducer = (state = [], action) => {
switch (action.type) {
case 'FETCH_ANIMALS':
return action.payload;
case 'ADD_ANIMALS':
return [...state, action.payload];
case 'DELETE_ANIMALS':
return [
...state.filter(item => item.id !== action.payload)
];
case 'UPDATE_ANIMALS':
return [
NOT SURE WHAT TO PUT HERE
];
default:
return state;
}
};
【问题讨论】:
-
我试过了,但我不确定我做得对。只是得到一个未定义的错误。
-
您可能还有其他不正确的地方,但没有minimal reproducible example,我们只能猜测!