React Native - 未定义不是对象(评估“Items.Length”)

Ayu*_*wal 7 react-native react-native-sectionlist

我无法解决与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: '',
      },
    ],
  },
];
Run Code Online (Sandbox Code Playgroud)

我正在尝试实现SectionList,data上面传递的常量值在哪里。

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

但我收到错误:未定义不是对象(评估“Items.Length”)

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

Bry*_*ruk 13

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

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

<SectionList
  sections={DATA}
/>
Run Code Online (Sandbox Code Playgroud)


小智 1

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

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