【问题标题】:React Native - Undefined Is Not An Object(Evaluating 'Items.Length')React Native - 未定义不是对象(评估'Items.Length')
【发布时间】:2020-09-24 14:03:56
【问题描述】:

我无法解决与 SectionList - React-native 组件相关的问题。

这是我的数据结构:

[
  {
    id: 'newKey1',
    image: '',
    title: 'Men Clothing',
    subCategory: [
      {
        id: 'key1',
        displayName: 'value1',
        image: '',
      },
      {
        id: 'key2',
        displayName: 'value2',
        image: '',
      },
    ],
  },
  {
    id: 'newKey2',
    image: '',
    title: 'Women Clothing',
    subCategory: [
      {
        id: 'key1',
        displayName: 'value1',
        image: '',
      },
      {
        id: 'key2',
        displayName: 'value2',
        image: '',
      },
      {
        id: 'key3',
        displayName: 'value3',
        image: '',
      },
    ],
  },
];

我正在尝试实现 SectionList,其中data 是上面传递的 const 值。

 return (
<View style={styles(theme).container}>
        <AdBanner />
        <SectionList sections={data} />
      </View>
);

但我得到一个错误:未定义不是对象(评估'Items.Length')

请帮助并分享可能的解决方案 谢谢你

【问题讨论】:

    标签: react-native react-native-sectionlist


    【解决方案1】:

    @Bryson Kruk 的回答是正确的。但是即使在执行了必要的步骤之后,我也遇到了同样的错误,即拥有这种格式的数据;

    const DATA = [{
    title: "Some Title"
    data : ["Some string", "Some other string"]
    }];
    

    SectionList 有道具sections

    <SectionList
      sections={DATA}
    />
    

    但这里要注意的另一点是,数据的值(存在于 DATA 中)必须是一个列表。我的意思是,假设您正在尝试获取某些计算的数据,有时计算的值可能是null 或除列表之外的任何其他内容。当时它也给出了这样的错误。

    所以为了摆脱这种错误,我们应该在没有数据时将其值设置为空列表,而不是将其设置为null或任何其他值。

    【讨论】:

    • “必须是字符串”要求不正确,或者至少不再正确(ReactNative 0.63.4)。我的数据数组中的元素是对象,它工作正常。
    • @ChrisBartley 对不起。那是一个错字。我的意思不是说我们不应该将数据设为空。当我们没有数据或有一些元素时,它应该是一个空列表。
    【解决方案2】:

    事实证明,SectionLists 需要一个 section 属性,它需要一个对象数组,这些对象需要一个数据属性,这是一个数组。这在文档中很容易被忽略。

    const DATA = [{
    title: "Cake"
    data : ["Some string", "Some other string"]
    }];
    
    <SectionList
      sections={DATA}
    />
    

    【讨论】:

      【解决方案3】:

      根据文档,您的数据必须采用Array&lt;Section&gt; 的形式,其中Section 包含Data 数组属性。

      更多信息:https://reactnative.dev/docs/sectionlist#section

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-09-29
        • 2019-04-05
        • 2021-11-09
        • 1970-01-01
        相关资源
        最近更新 更多