4

我无法解决与 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')

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

4

3 回答 3

4

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

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

<SectionList
  sections={DATA}
/>
于 2021-05-25T19:11:16.847 回答
0

根据文档,您的数据必须采用 form Array<Section>,其中Section包括DataArray 属性。

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

于 2020-06-05T11:27:31.723 回答
0

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

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

SectionList有道具sections

<SectionList
  sections={DATA}
/>

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

因此,为了消除此类错误,我们应该在没有数据时将其值设置为空列表,而不是将其设置为null或任何其他值。

于 2021-07-15T13:52:10.363 回答