Reputation: 9885
I have a list of items inside a Flatlist and I want to separate them with a point (‧) like this:
I have something like this: (snack.expo.io/@abranhe/flatlist-separator)
<FlatList
data={[ 'foo', 'bar', 'hello', 'word' ]}
renderItem={({ item }) => <Text>{item} ‧ </Text>}
horizontal={true}
keyExtractor={(item, index) => index.toString()}
/>
Unfortunately, it will add an extra point (‧) at the end of the items.
foo ‧ bar ‧ hello ‧ world ‧
Is there a simple way to remove the last separator from the list?
foo ‧ bar ‧ hello ‧ world
Thanks in advance!
Upvotes: 2
Views: 1706
Reputation: 1194
<FlatList
data={data}
horizontal
renderItem={({ item }) => <Text>{item}</Text>}
keyExtractor={(item, index) => index.toString()}
ItemSeparatorComponent={() => <Text> ‧ </Text>}
/>
Upvotes: 2
Reputation: 24680
renderItem
prop has a property called index
which tells you the index of the current rendering item. You can check that to see if it is the last item.
Example
<FlatList
data={this.state.someData}
renderItem={({ item, index }) => (
<Text>{(index !== (this.state.someData.length -1) ? `${item} -` : item)} </Text>
)}
horizontal={true}
keyExtractor={(item, index) => index.toString()}
/>
Upvotes: 0