0

我为以下 JSON 创建了名为 ListItems 的组件。该组件在区域和郊区数据之间共享。

如果我选择 Region 1 和 Region 2(都有两个郊区),它工作正常。但是,一旦我选择了拥有 10 个郊区的 Region 3,它就会抛出 undefined is not an object (evalating 'arrs[index].isChecked') 错误。当我执行 console.log(arrs) 时,它会保持先前选择的区域的状态。当区域选择发生变化时,如何重置状态 arrs?它保持旧状态的数组长度。

我在这里创建了小吃:https ://snack.expo.dev/@everestster/listitem-component

任何帮助将不胜感激。

[
  {
    id: 1,
    name: 'Project 1',
    regions: [
      {
        id: 1,
        name: 'Region 1',
        type: 'region',
        itemsCount: 200781,
        suburbs: [
          {
            id: 1,
            name: 'Suburb 1',
            type: 'suburb',
            itemsCount: 120,
          },
          {
            id: 2,
            name: 'Suburb 2',
            type: 'suburb',
            itemsCount: 120,
          },
        ],
      },
      {
        id: 2,
        name: 'Region 2',
        itemsCount: 149814,
        type: 'region',
        suburbs: [
          {
            id: 3,
            name: 'Suburb 3',
            type: 'suburb',
            itemsCount: 1,
          },
          {
            id: 4,
            name: 'Suburb 4',
            type: 'suburb',
            itemsCount: 1,
          },
        ],
      },
      {
        id: 3,
        name: 'Region 3',
        type: 'region',
        itemsCount: 230711,
        suburbs: [
          {
            id: 5,
            name: 'Suburb 5',
            type: 'suburb',
            itemsCount: 1417,
          },
          {
            id: 6,
            name: 'Suburb 6',
            type: 'suburb',
            itemsCount: 54,
          },
          {
            id: 101,
            name: 'Suburb 101',
            type: 'suburb',
            itemsCount: 34,
          },
          {
            id: 102,
            name: 'Enmore',
            type: 'suburb',
            itemsCount: 6579,
          },
          {
            id: 103,
            name: 'Subutb 103',
            type: 'suburb',
            itemsCount: 1730,
          },
          {
            id: 104,
            name: 'Suburb 104',
            type: 'suburb',
            itemsCount: 9443,
          },
          {
            id: 105,
            name: 'Suburb 105',
            type: 'suburb',
            itemsCount: 1730,
          },
          {
            id: 105,
            name: 'Hornsby',
            type: 'suburb',
            itemsCount: 14418,
          },
          {
            id: 106,
            name: 'Suburb 106',
            type: 'suburb',
            itemsCount: 1620,
          },
          {
            id: 107,
            name: 'Suburb 107',
            type: 'suburb',
            itemsCount: 14418,
          },
        ],
      },
      {
        id: 4,
        name: 'Region 4',
        type: 'region',
        itemsCount: 120852,
        suburbs: [
          {
            id: 7,
            name: 'Suburb 7',
            type: 'suburb',
            itemsCount: 1,
          },
          {
            id: 8,
            name: 'Suburb 8',
            type: 'suburb',
            itemsCount: 1,
          },
        ],
      },
      {
        id: 5,
        name: 'Region 5',
        type: 'region',
        itemsCount: 234840,
        suburbs: [
          {
            id: 9,
            name: 'Suburb 9',
            type: 'suburb',
            itemsCount: 1,
          },
          {
            id: 10,
            name: 'Suburb 10',
            type: 'suburb',
            itemsCount: 1,
          },
        ],
      },
    ],
  },
  {
    id: 2,
    name: 'Project 2',
    regions: [
      {
        id: 11,
        name: 'Region 11',
        type: 'region',
        suburbs: [],
      },
      {
        id: 12,
        name: 'Region 12',
        type: 'region',
        suburbs: [],
      },
    ],
  },
  {
    id: 3,
    name: 'Project 3',
    regions: [],
  },
  {
    id: 4,
    name: 'Project 4',
    regions: [],
  },
  {
    id: 5,
    name: 'Project 5',
    regions: [],
  },
];
4

0 回答 0