React Native — Nested Virtualization Anti-pattern (Performance Optimization)
ERROR “ VirtualizedLists should never be nested inside plain ScrollViews”.
React Native provides some components for rendering large lists of data efficiently . These components are based on the VirtualizedList component, which implements a virtualization technique to improve memory consumption and performance.
Virtualization means that only the items that are currently visible on the screen (or within a certain window size) are rendered, while the rest are replaced by blank spaces of the same size. This way, the list can handle thousands of items without affecting the app’s responsiveness or memory usage.
Nested VirtualizedLists Error
However, there is a common mistake that developers make when using these components: nesting them inside a plain ScrollView with the same orientation (horizontal or vertical). Ultimately it creates a silent error in metro like bellow:
Error: ? VirtualizedLists should never be nested inside plain ScrollViews ?
Let’s see an example of Error View (FlatList inside ScrollView):
Error Reason
So, what are the problems when you nested a VirtualizedList component inside a plain ScrollView? Why was that error sent to your metro?
There are several reasons why nested VirtualizedList is an Anti-pattern. Such as:
Quick solution before details explanation
To avoid these problems, React Native warns you when you nest a VirtualizedList inside a plain ScrollView with the same orientation, and suggests you to use another VirtualizedList-backed container instead. This means that you should either:
By following these suggestions, you can ensure that your lists are rendered efficiently and smoothly, without compromising the user experience or the app performance.
Let’s get started with details explanation & solution.
Error Code
So, in my case I had a code a where I needed to show a list of Images in FlatList and at the top of the list there was a Title & a camera button to take more photos. The total view was wrapped by a ScrollView. Let’s see this anti-pattern view ??
领英推荐
So, from the above picture we see that FlatList (red box) was wrapped by a ScrollView (green box). This is Anti-Pattern.
Now let’s see this anti-pattern code ??
Solution Code
We can solve it by using only the FlatList instead of using both FlatList with ScrollView. Flatlist made it really simple by providing the support for these two props functions. These 2 are:
So we will use ListHeaderComponent props function to solve our above problem for which the nested FlatList was used.
So, basically our problem was that we needed to make the header (Tittle & camera button) as scrollable. So, to do that we used ScrollView. But now we can do that by using FlatList ListHeaderComponent props function.
So, what we will do here is Just Wrap that header (Title & Camera button) into a component & then pass that component into FlatList ListHeaderComponent props function.
Let’s see the code bellow. First, create a wrapper component for Title & Camera button like bellow.
Now just add this gearHeader() function as the ListHeaderComponent props of FlatList like bellow
Here is the view of the solution
Here is the final code with ListHeaderComponent props function