Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

FlatList vs map react-native

I have recently started using react-native and have come across the FlatList component. When using react I have always used map with an array.

I was using FlatList but there was issues when I wanted to change the flex-direction of items in the FlatList so I reverted to using map.

Here are the two examples using both methods:

map

{
    this.state.images.map(image => {
       return (
          <UsersImage key={ image } source={{ uri: image }} />
        )
    })
}

FlatList

<FlatList
    data={ this.state.images }
    renderItem={({item}) => {
        return (
            <UsersImage source={{ uri: item }} />
        )
    }}  
   keyExtractor={(item, index) => index}
/>

Can anyone explain why one should use FlatList over map or vice versa?

like image 265
peter flanagan Avatar asked Jan 04 '18 21:01

peter flanagan


1 Answers

FlatList has lazy loading (it only shows what's on screen, so it can be more performant if you have a huge list). BTW you can get it to be horizontal if that's what you needed to change the flex direction for - just pass the horizontal prop (equivalent to saying horizontal={true})

like image 130
R u c k s a c k Avatar answered Sep 21 '22 06:09

R u c k s a c k