I've 4 FlatList
s with maxHeight
set to 200
inside a ScrollView
.
<ScrollView> <FlatList/> <FlatList/> <FlatList/> <FlatList/> </ScrollView>
and when I try to scroll a FlatList
, it doesn't scroll but the ScrollView
scrolls. How do I fix this issue ?
Full Source Code
import { Component, default as React } from 'react'; import { FlatList, ScrollView, Text } from 'react-native'; export class LabScreen extends Component<{}> { render() { return ( <ScrollView> {this.renderFlatList('red')} {this.renderFlatList('green')} {this.renderFlatList('purple')} {this.renderFlatList('pink')} </ScrollView> ); } getRandomData = () => { return new Array(100).fill('').map((item, index) => { return { title: 'Title ' + (index + 1) }; }); }; renderFlatList(color: string) { return ( <FlatList data={this.getRandomData()} backgroundColor={color} maxHeight={200} marginBottom={50} keyExtractor={(item, index) => index.toString()} renderItem={({ item }) => <Text>{item.title}</Text>} /> ); } }
snack.expo link
Under the hood, FlatList uses the ScrollView component to render scrollable elements. However, unlike generic ScrollView, FlatList displays data lazily to save memory and processing time.
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.
ScrollView renders all its react child components at once, but this has a performance downside. FlatList renders items lazily, when they are about to appear, and removes items that scroll way off-screen to save memory and processing time.
To enable or disable scrolling on FlatList with React Native, we can set the scrollEnabled prop. to set the scrollEnabled prop to false to disable scrolling on the FlatList.
We can use the built-in nestedscrollenabled prop for the children FlatList/ScrollView components.
<FlatList nestedScrollEnabled />
This is only required for Android (Nested scrolling is supported by default on iOS).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With