简体   繁体   English

React-Native Flatlist renderItem

[英]React-native flatlist renderItem

I have a array dictionary. 我有一个数组字典。 I want to render all value to flatlist. 我想将所有价值呈现给清单。 I try: 我尝试:

renderItem = {({item}) => item.A.id} renderItem = {({item})=> item.A.id}

But I don't return any value. 但是我没有返回任何价值。 Please help me. 请帮我。 thanks 谢谢

ArrAccount: {
  "A": [
    {
      "id": 1,
      "name": "Anh Tuan Nguyen",
      "age": 28
    },
    {
      "id": 2,
      "name": "An Nhien",
      "age": 2
    },
  ],
  "Z": [
    {
      "id": 3,
      "name": "Thanh Tu Pham",
      "age": 32
    },
    {
      "id": 4,
      "name": "Tien Thanh",
      "age": 24
    },
  ]
}

If your this.props.myArrayAccount field like this 如果您的this.props.myArrayAccount字段像这样

this.props.myArrayAccount = {
        ArrAccount: {
          "A": [
            {
              "id": 1,
              "name": "Anh Tuan Nguyen",
              "age": 28
            },
            {
              "id": 2,
              "name": "An Nhien",
              "age": 2
            },
          ],
          "Z": [
            {
              "id": 3,
              "name": "Thanh Tu Pham",
              "age": 32
            },
            {
              "id": 4,
              "name": "Tien Thanh",
              "age": 24
            },
          ]
        }
      }

const myArrayAccount = this.props.myArrayAccount.ArrAccount
const ArrAccount = Object.keys(myArrayAccount).map((key)=> myArrayAccount[key])

because your props has more than one object inside it you should should map the object first than map the Flatlist component 因为props内部有多个对象,所以应该先map对象,而不是映射Flatlist组件

you your data in FlatList component should like this: 您在FlatList组件中的数据应如下所示:

{ArrAccount.map((item) =>
    <FlatList
      data={item}
      renderItem={({item}) => <Text>{item.name}</Text>}
    />
  )
}

You should use the renderItem like this: 您应该这样使用renderItem

renderItem = {({item:data}) => <Text>data.id</Text>}

And this will list all the ids. 这将列出所有ID。

Note: this style is so simple you can customize the style of items. 注意:此样式非常简单,您可以自定义项目的样式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM