【问题标题】:Redux state disappears after inserting into a nested array插入嵌套数组后 Redux 状态消失
【发布时间】:2021-08-05 15:45:28
【问题描述】:

当尝试插入我的 redux 状态时,我的状态消失了。我正在检查我是否有 parentId,如果有,我插入到 parent id 子数组中,但如果没有提供 parent id,我只需将有效负载插入到数组中。最后一部分工作正常,问题在于添加到子数组: 这是我的代码L:

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;
  }
};

【问题讨论】:

  • 能否请您也将分类模型粘贴到问题中

标签: javascript reactjs redux redux-thunk


【解决方案1】:

CREATE_CATEGORY 案例中的第一个条件实际上并没有返回任何内容。 您可以尝试使用在第二种情况下已经完成的相同方法,即:

[
  ...(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: [],
            },
          ];
        }
      }))
  ]

最终代码:

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;
  }
};

更新

从代码中,我假设父级和子级迭代之间存在混淆。如果父对象是包含children 属性的对象的数组,那么您会这样做:

 return {
    ...category,
    children: [
       ...category.children,
       {
              _id: action.payload._id,
              name: action.payload.name,
              slug: action.payload.slug,
              children: [],
       },
    ]
 }

【讨论】:

  • 这确实回答了我的问题,谢谢!如果没问题,我确实有后续行动。使用以下代码,我该如何更新 category.children 。假设我想在 children 数组中插入一个新类别,但当我尝试这样做时,它并不完全奏效。
猜你喜欢
  • 2017-04-30
  • 2021-05-23
  • 2018-03-05
  • 2021-04-15
  • 1970-01-01
  • 2019-01-28
  • 2018-01-21
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多