Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ERROR - VirtualizedLists should never be nested inside plain ScrollViews with the same orientation

Tags:

react-native

I'm working on a react-native app and I have to put a list of object in a Scrollview, so I use the FlatList component to do it. This is the piece of code that generates the error:

<ScrollView contentContainerStyle={style}>
   Other components
   <FlatList
       style={style}
       data={data}
       scrollEnabled={false}
       keyExtractor={(item, index) => index.toString()}
       renderItem={({ item, index}) => (somethings)}
   />
   Other components
</ScrollView>

The complete error is: VirtualizedLists should never be nested inside plain ScrollViews with the same orientation because it can break windowing and other functionality - use another VirtualizedList-backed container instead.

like image 943
Alessandro Carughi Avatar asked May 20 '21 15:05

Alessandro Carughi


People also ask

How do you fix VirtualizedLists should never be nested inside plain ScrollViews?

The fix to this warning is more straightforward than you think: get rid of the ScrollView, and places all the components that surround the FlatList inside ListFooterComponent and ListHeaderComponent. ); }; Both props only accept one component, so we wrapped the components in the ListHeaderComponent with Fragments.

Should never be nested inside plain ScrollViews with the same orientation because?

VirtualizedLists should never be nested inside plain ScrollViews with the same orientation because it can break windowing and other functionality (not warning) #33024. Component: ScrollView Component: VirtualizedList Needs: Triage 🔍 Stale.

Can I use FlatList inside ScrollView?

"VirtualizedLists should never be nested inside plain ScrollViews with the same orientation" error in console for FlatList/SectionList with scrollEnabled={false}

What is the difference between FlatList and SectionList?

SectionList s are like FlatList s, but they can have section headers to separate groups of rows. SectionList s render each item in their input sections using the renderSectionHeader and renderItem prop. Each item in sections should be an object with a unique id (the key), and an array data of row data.


2 Answers

Flatlist has its own ScrollView you can scroll through the list using that so there is no need to put a flatlist into a ScrollView that is why its giving a warning, the both scrollview will clash and one of them (mostly the parent one) works.

like image 135
Kulsoom Irshad Avatar answered Sep 21 '22 07:09

Kulsoom Irshad


The error is self explanatory and it should be in a developers best interest to avoid these kind of things even when it's just a false alarm. Your particular situation could use the following solution:

<FlatList
  data={data}
  keyExtractor={(item, index) => `key-${index}`}
  ListHeaderComponent={() => (
    <SomeComponents>
      ...Some components those need to be on top of the list
    </SomeComponents>
  )}
  ListFooterComponent={() => (
    <SomeComponents>
      ...Some components those need to be below the list
    </SomeComponents>
  )}
  renderItem={({ item, index}) => (somethings)}
/>

Another note, if you need more complex list that needs header and footer for the list itself, you can try SectionList.

like image 35
rule_it_subir Avatar answered Sep 19 '22 07:09

rule_it_subir