Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

FlatList inside ScrollView in React-Native? [closed]

I'm learning to use React Native by creating an application, so I'm creating a scrollable homepage and inside it 10 rectangles with different names. If I understand correctly, you need to use flatlist to manage the scrolling of these rectangles, but I already have a scrollview within the same page. I have something like this:

<ScrollView> 
  <Image/>
  <Text/>
  <RectangleComponents/>
</ScrollView>

Inside RectanglesComponent is the code that creates the 10 rectangles. I'm currently using a ScrollView here too because I couldn't use FlatList inside a ScrollView. Example of RectanglesComponent:

    return (
    <SafeAreaView style={styles.container}>
        {Array.from(Array(10).keys()).map(i => ( 
            <SafeAreaView key={i} style={styles.rectangle}/>
    </SafeAreaView>
    );

In this case, I need to know if FlatList or ScrollView is good?

like image 960
frak01 Avatar asked Nov 30 '25 16:11

frak01


1 Answers

You should choose either ScrollView or FlatList

According to React Native docs (https://reactnative.dev/docs/scrollview):

<ScrollView> vs <FlatList> - which one to use?

ScrollView renders all its react child components at once, but this has a performance downside.

Imagine you have a very long list of items you want to display, maybe several screens worth of content. Creating JS components and native views for everything all at once, much of which may not even be shown, will contribute to slow rendering and increased memory usage.

This is where FlatList comes into play. FlatList renders items lazily, when they are about to appear, and removes items that scroll way off screen to save memory and processing time.

FlatList is also handy if you want to render separators between your items, multiple columns, infinite scroll loading, or any number of other features it supports out of the box.

Reading your requirement, FlatList is the best and easiest way to implement.

like image 141
Hanief Utama Avatar answered Dec 03 '25 06:12

Hanief Utama



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!