Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React FlatList renderItem

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?

like image 293
Julian Avatar asked Aug 01 '17 05:08

Julian


People also ask

What is renderItem in FlatList?

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.

What is renderItem?

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.

How do you remove a item index from a FlatList in React Native?

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.

How do you optimize a large list of items on FlatList?

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.


2 Answers

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
        )}
      />
 );
}
like image 70
Balasubramanian Avatar answered Sep 17 '22 14:09

Balasubramanian


 <ListItem
    data={this.state.data}
    key={(item,index)=>index.toString()}
    renderItem={({ item }) => <YourComponent item={item} /> }
  />
like image 21
Idan Avatar answered Sep 20 '22 14:09

Idan