I've seen this sort of syntax in JS before and I am just curious how it works. In the React Native Docs for FlatList, an example calls renderItem. How does this._renderItem know that what individual list item it is working with? It looks like item is being destructured, but from what object?
_renderItem = ({item}) => (
<MyListItem
id={item.id}
onPressItem={this._onPressItem}
selected={!!this.state.selected.get(item.id)}
title={item.title}
/>
);
render() {
return (
<FlatList
data={this.props.data}
extraData={this.state}
keyExtractor={this._keyExtractor}
renderItem={this._renderItem}
/>
);
}
Put differently: in FlatList, another way to make this same call could be:
<FlatList <other props> renderItem={({item}) => (<MyListItem ....) />
Is renderItem some special prop where {item} will always be the destructured arg?
renderItem renderItem({ item, index, separators }); Takes an item from data and renders it into the list. Provides additional metadata like index if you need it, as well as a more generic separators.
renderItem is a function that requires returning the component that will display the data in the list. keyExtractor – This is a prop responsible for taking an individual item of our data as an argument and returning a value used as a key. The id will be used as a key.
Now let's understand it, First of all we would get the selected item Id on item click event, Now we would use Data. filter() method with selected ID and remove the selected id then update the Data again using State. This would remove selected item from FlatList in react native.
Use basic components The more complex your components are, the slower they will render. Try to avoid a lot of logic and nesting in your list items. If you are reusing this list item component a lot in your app, create a component only for your big lists and make them with as little logic and nesting as possible.
data prop - need to pass an array of data to the FlatList via the data prop
. That’s available on this.props.data.
renderItem prop - Then you want to render the content with the renderItem
prop. The function is passed a single argument, which is an object. The data you’re interested in is on the item key
so you can use destructuring to access that from within the function. Then return a component using that data.
render() {
return (
<FlatList
data={this.state.data}
renderItem={({ item }) => (
// return a component using that data
)}
/>
);
}
<ListItem
data={this.state.data}
key={(item,index)=>index.toString()}
renderItem={({ item }) => <YourComponent item={item} /> }
/>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With