Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ReactNative Flatlist - RenderItem not working

Tags:

So I'm trying to use React Native's FlatList renderItem property, but something very strange is happening.

The data property is set to an array which has elements which are not undefined, but then, in the renderItem function, it gives me an error saying that the argument of the function is undefined, unless I call the argument item.

Here's my code:

export default class Profile extends React.Component {
    onLearnMore = (user) => {
        this.props.navigation.navigate('UserDetail', user)
    }

    render() {
        return (
            <List>
                <FlatList
                    data={data.users}
                    renderItem={( {item} ) => {
                        console.log(item)
                        return (<ListItem
                            roundAvatar
                            title={`${item.fName} ${item.lName}`}
                            onPress={() => this.onLearnMore(item)}
                        />)
                    }}
                />
            </List>
        )
    }
}

If I swapped {item} with {userData}, then userData would be undefined later in the function. Does anyone know why this happens?

like image 754
Corrado Avatar asked Feb 24 '18 17:02

Corrado


People also ask

Why FlatList is not working in React Native?

To fix the React Native FlatList last item not visible issue, we can add bottom padding to the FlatList. to add a FlatList and set its paddingBottom style to 50 pixels to add some padding at the bottom of the FlatList. We render the item with the renderitem function to render each DATA entry.

How do I use extra data on FlatList?

flatlist-simpleBy passing extraData={selectedId} to FlatList we make sure FlatList itself will re-render when the state changes. Without setting this prop, FlatList would not know it needs to re-render any items because it is a PureComponent and the prop comparison will not show any changes.

How do I get the index of FlatList?

To get the index of the currently visible item in a React Native flat list, we can set the onViewableItemsChange prop to a function that gets the current visible items. to define the onViewableItemsChanged function that gets the viewableItems property. viewableItems has the items that are current visible on the screen.

Why FlatList React Native to keyExtractor?

Conclusion. When using a FlatList component, if the data array has a unique id or a key property, you do not need to use the keyExtractor prop explicitly. But for custom id names, use the keyExtractor prop to explicitly tell the component which unique key to extract.


4 Answers

Reason is that, every object in the data array is referenced through item property of the actual parameter passed to renderItem function. Here, you are using object destructuring to extract only item property of the passed in object, thats why u are using {item}. When you are changing this name to userData (which is missing in the function argument), you are getting undefined. Have a look at the function signature of renderItem here.

If you want to use userData instead of item, then you can rename item to userData as

renderItem= ({item: userData}) => {...}

Hope this will help!

like image 160
Prasun Avatar answered Sep 23 '22 13:09

Prasun


Please read this answer carefully. I experienced it and wasted many hours to figure out why it was not re-rendering:

We need to set extraData prop of FlatList if there is any change in the state like so:

<FlatList data={this.state.data} extraData={this.state} .../>

Please see the official documentation here:

https://facebook.github.io/react-native/docs/flatlist.html

like image 29
atulkhatri Avatar answered Sep 25 '22 13:09

atulkhatri


   renderItem={({ item, index }) => {
                return (
              <Text>{item.fName + item.lName}</Text>
                );
              }}
like image 2
Antier Solutions Avatar answered Sep 27 '22 13:09

Antier Solutions


I was missing a curlybraces { } around the item. After adding them , now it work fine.

renderItem= {({item}) => this.Item(item.title)}
like image 1
Pravin Ghorle Avatar answered Sep 27 '22 13:09

Pravin Ghorle