TechnicalEasy React Native
When should you use FlatList instead of ScrollView in React Native?
Asked for Mobile Developer
Why they ask this: Long lists are the most common performance problem in React Native apps.
Answer
ScrollViewrenders all of its children at once. That is fine for short, fixed content such as a settings screen.FlatListis virtualised: it renders only the items near the screen and recycles the rest. Use it for long or unknown-length lists such as feeds, job lists and chat.
jsx
<FlatList
data={jobs}
keyExtractor={(job) => String(job.id)}
renderItem={({ item }) => <JobCard job={item} />}
onEndReached={loadMore} // infinite scroll
onEndReachedThreshold={0.5}
refreshing={refreshing}
onRefresh={refresh} // pull to refresh
/>Use SectionList for grouped data. For extra speed, memoise the item component, give a stable keyExtractor, and use getItemLayout when every row has the same height. FlashList (from Shopify) is a popular faster alternative.
Can you explain this in your own words?