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?
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.
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.
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.
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.
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!
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
renderItem={({ item, index }) => {
return (
<Text>{item.fName + item.lName}</Text>
);
}}
I was missing a curlybraces { } around the item. After adding them , now it work fine.
renderItem= {({item}) => this.Item(item.title)}
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