redux - 如何删除嵌套到另一个数组的数组中的元素
redux - how to remove element in array which is nested to another array
我想通过发送操作“itemRemoved”来删除项目,我将板的 ID 和要删除的元素的 ID 传递给它
如何编辑我的 Reducer“案例 ITEM_REMOVED”?
const initialState = {
boards: [
{
id: 1,
title: "1",
items: [
{ id: 1, title: "1" },
{ id: 2, title: "2" },
],
},
{
id: 2,
title: "2",
items: [
{ id: 3, title: "3" },
{ id: 4, title: "4" },
{ id: 5, title: "5" },
],
},
],
};
const actions = {
itemRemoved: (boardId, itemId) =>
({ type: ITEM_REMOVED, boardId, itemId }),
}
const boardsReducer = (state = initialState, action) => {
switch (action.type) {
case ITEM_REMOVED:
return {
...state,
boards: [] // <-- ??????????
};
default:
return state;
}
};
我会尝试这样的事情:
const boardsReducer = (state = initialState, action) => {
switch (action.type) {
case ITEM_REMOVED:
return {
...state,
boards: state.boards.map(b => {
if (b.id !== action.boardId) {
return b
}
const newB = {...b}
newB.items = newB.items.filter(i => i !== action.itemId)
return newB;
})
};
default:
return state;
}
};
我想通过发送操作“itemRemoved”来删除项目,我将板的 ID 和要删除的元素的 ID 传递给它
如何编辑我的 Reducer“案例 ITEM_REMOVED”?
const initialState = {
boards: [
{
id: 1,
title: "1",
items: [
{ id: 1, title: "1" },
{ id: 2, title: "2" },
],
},
{
id: 2,
title: "2",
items: [
{ id: 3, title: "3" },
{ id: 4, title: "4" },
{ id: 5, title: "5" },
],
},
],
};
const actions = {
itemRemoved: (boardId, itemId) =>
({ type: ITEM_REMOVED, boardId, itemId }),
}
const boardsReducer = (state = initialState, action) => {
switch (action.type) {
case ITEM_REMOVED:
return {
...state,
boards: [] // <-- ??????????
};
default:
return state;
}
};
我会尝试这样的事情:
const boardsReducer = (state = initialState, action) => {
switch (action.type) {
case ITEM_REMOVED:
return {
...state,
boards: state.boards.map(b => {
if (b.id !== action.boardId) {
return b
}
const newB = {...b}
newB.items = newB.items.filter(i => i !== action.itemId)
return newB;
})
};
default:
return state;
}
};