React Native FlatList has similar structure to your List
component and it's highly recommended by react native community to use FlatList
because of efficiency, index
of rendered item is accessible easily. here is an simple usage for what you want:
render() {
return (
<FlatList
data={[
{title: "title1"},
{title: "title2"},
{title: "title3"},
{title: "title4"},
{title: "title5"},
{title: "title6"},
{title: "title7"},
{title: "title8"},
]}
renderItem={(item) => <Text>{item.index}</Text>}
keyExtractor={(item, index) => index.toString()}/>
);
}
item
parameter in renderItem
has two important property.
- index: index of item being rendered
- data: the data you provided to flat list, in this sample is one of objects of array passed to
data
props of flatList, e.g. {title: "title1"}
that I don't use this data in renderItem
method. you can do what you want with it in your way like this: data={this.props.groceries}