【发布时间】:2020-08-01 16:34:50
【问题描述】:
我正在尝试删除待办事项,我想从具有特定 ID 的对象“byIds”中删除该项目。 它就像数组的过滤器,但对象的过滤器。 我不知道有什么复杂的希望得到帮助我相信它很愚蠢
import { ADD_TODO, TOGGLE_TODO, DELETE_TODO } from "../actionTypes";
const initialState = {
allIds: [],
byIds: {},
};
export default function (state = initialState, action) {
switch (action.type) {
case ADD_TODO: {
const { id, content } = action.payload;
return {
...state,
allIds: [...state.allIds, id],
byIds: {
...state.byIds,
[id]: {
content,
completed: false,
},
},
};
}
case TOGGLE_TODO: {
const { id } = action.payload;
return {
...state,
byIds: {
...state.byIds,
[id]: {
...state.byIds[id],
completed: !state.byIds[id].completed,
},
},
};
}
// of course its toggling but it doesn't even get there
case DELETE_TODO: {
const { id } = action.payload;
return {
...state,
allIds: state.allIds.filter((todo) => todo !== id),
byIds: state.byIds.filter((todo) => todo !== id),
};
}
default:
return state;
}
}
{
todos: {
allIds: [
1,
2,
3,
4
],
byIds: {
'1': {
content: 'Test1',
completed: false
},
'2': {
content: 'Test2',
completed: false
},
'3': {
content: 'test3',
completed: false
},
'4': {
content: 'test4',
completed: false
}
}
},
visibilityFilter: 'all'
}
对于要求我控制台记录 byIds 的人希望对我有所帮助
【问题讨论】:
-
你能分享
console.log(byIds)的数据形状吗?添加到帖子中。 -
Redux 对于这个问题来说是不必要的,如果你把它抽象出来做出反应会更容易,或者更简单的是 JS。
-
@NicolasHevia 清除标签
-
@BARNOWL 我做到了希望得到帮助,我只想删除一个待办事项
-
以下任何答案有帮助吗?
标签: javascript reactjs