Home > Net >  How to put value from an object inside an array into a flatlist React Native
How to put value from an object inside an array into a flatlist React Native

Time:01-04

so I have this array:

  let Reviews = [
    {
      name: "Tibo Mertens",
      review: "Very nice model",
    },
    {
      name: "Quintt Adam", 
      review: "I like the car",
    },
  ];

And a Flatlist:

    <View>
      <FlatList
        data={Reviews}
        renderItem={({ item }) => {
          <View>
            <Text>{item.name}</Text>
          </View>;
        }}
      ></FlatList>
    </View>

Where you see {item.name} I want to display the names of the objects in the flatlist. But nothing is showing, the value is undefined. How do I get the right values?

CodePudding user response:

There are a few things wrong with your code.

  1. You are not returning the View in renderItem.
  2. You having a semi-colon on the end of the View in renderItem - You should remove that.
  3. FlatList should be self-closing.
<View>
  <FlatList 
    data={Reviews}
    renderItem={({ item }) => 
      <View><Text>{item.name}</Text></View>
    }
  />
</View>

To note, the above is an implicit return and is an alternative to the following:

renderItem={({ item }) => { 
  return <View><Text>{item.name}</Text></View>
}}
  • Related