[英]Redux state disappears after inserting into a nested array
When trying to insert into my redux state, my state disappears,.当试图插入我的 redux state 时,我的 state 消失了。 I am checking to see if I have a parentId and if I do, I insert into the parents id children array, but if no parent id is provided, I simply insert the payload into the array.我正在检查我是否有 parentId,如果有,我插入到 parent id 子数组中,但如果没有提供 parent id,我只需将有效负载插入到数组中。 The last part works fine, the problem is with adding to the child array: Here is my codeL:最后一部分工作正常,问题在于添加到子数组:这是我的代码:
export default (categories = [], action) => { switch (action.type) { case 'GET_ALL_CATEGORIES': return action.payload; // I am having a problem with the case below case 'CREATE_CATEGORY': return action.payload.parentId? categories.map((category) => { if ( category._id.toString() === action.payload.parentId.toString() ) { return [...category.children, { _id: action.payload._id, name: action.payload.name, slug: action.payload.slug, children: [], }, ]; } else { return [ { _id: action.payload._id, name: action.payload.name, slug: action.payload.slug, children: [], }, ]; } }): [...categories, { _id: action.payload._id, name: action.payload.name, slug: action.payload.slug, children: [], }, ]; default: return categories; } };
The first condition within CREATE_CATEGORY
case, doesn't really return anything. CREATE_CATEGORY
案例中的第一个条件实际上并没有返回任何内容。 You can try using the same approach you've already done in the second condition, ie:您可以尝试使用在第二种情况下已经完成的相同方法,即:
[
...(categories.map((category) => {
if (
category._id.toString() === action.payload.parentId.toString()
) {
return [
...category.children,
{
_id: action.payload._id,
name: action.payload.name,
slug: action.payload.slug,
children: [],
},
];
} else {
return [
{
_id: action.payload._id,
name: action.payload.name,
slug: action.payload.slug,
children: [],
},
];
}
}))
]
Final code:最终代码:
export default (categories = [], action) => { switch (action.type) { case 'GET_ALL_CATEGORIES': return action.payload; // I am having a problem with the case below case 'CREATE_CATEGORY': return action.payload.parentId? [...(categories.map((category) => { if ( category._id.toString() === action.payload.parentId.toString() ) { return [...category.children, { _id: action.payload._id, name: action.payload.name, slug: action.payload.slug, children: [], }, ]; } else { return [ { _id: action.payload._id, name: action.payload.name, slug: action.payload.slug, children: [], }, ]; } })) ]: [...categories, { _id: action.payload._id, name: action.payload.name, slug: action.payload.slug, children: [], }, ]; default: return categories; } };
Update更新
From the code, I assume there has been a mixed up between parent and children iteration.从代码中,我假设父子迭代之间存在混淆。 If a parent is an array of an object containing children
property, then you would do:如果父级是包含children
级属性的 object 数组,那么您将执行以下操作:
return {
...category,
children: [
...category.children,
{
_id: action.payload._id,
name: action.payload.name,
slug: action.payload.slug,
children: [],
},
]
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.