最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - React-Native flatlist VirtualizedLists should never be nested inside plain ScrollViews - Stack Overflow

programmeradmin3浏览0评论

Hy, I'm new in react-native. I'm stuck in the flatlists and scrollView. Here's the structure of my code:

<View>
    <ScollView>
        <View>
            <SafeAreaView>
                <FlatList/> -- Horizontal Scroll --
            </SafeAreaView>
        </View>
        <View>
        </View>
        <View>
            <FlatList/> -- Horizontal Scroll --
        </View>
        <View>
            <FlatList/> -- Horizontal Scroll -- //Issue        
        </View>
    </ScollView>
</View>

So the issue flatlist mentioned above like //Issue. The issue is that when I add the flatlist which is need to scroll in vertical format its not scrolling and one more thing this flatlist is displaying at the middle of screen so to start the working I add the height and its start working but the functionality I want is that When I scroll the screen should move to down. So do this i add the scrollView as you in above structure but when I add scrollView I remove the height it start working perfectly but displaying that error:

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.

So to solved this I try:

  1. nestedScrollEnable={true}
  2. scrollEnabled
  3. giving height
  4. When I start working on this app I also face same issue then I get the solution using ListHeaderComponent and ListFooterComponent. But now my already code in Header and Footer ponent.

but I can't solve. If some have some idea then share it. I would appreciate your favor

Hy, I'm new in react-native. I'm stuck in the flatlists and scrollView. Here's the structure of my code:

<View>
    <ScollView>
        <View>
            <SafeAreaView>
                <FlatList/> -- Horizontal Scroll --
            </SafeAreaView>
        </View>
        <View>
        </View>
        <View>
            <FlatList/> -- Horizontal Scroll --
        </View>
        <View>
            <FlatList/> -- Horizontal Scroll -- //Issue        
        </View>
    </ScollView>
</View>

So the issue flatlist mentioned above like //Issue. The issue is that when I add the flatlist which is need to scroll in vertical format its not scrolling and one more thing this flatlist is displaying at the middle of screen so to start the working I add the height and its start working but the functionality I want is that When I scroll the screen should move to down. So do this i add the scrollView as you in above structure but when I add scrollView I remove the height it start working perfectly but displaying that error:

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.

So to solved this I try:

  1. nestedScrollEnable={true}
  2. scrollEnabled
  3. giving height
  4. When I start working on this app I also face same issue then I get the solution using ListHeaderComponent and ListFooterComponent. But now my already code in Header and Footer ponent.

but I can't solve. If some have some idea then share it. I would appreciate your favor

Share Improve this question asked Jun 15, 2021 at 17:21 Adil IjazAdil Ijaz 3521 gold badge5 silver badges26 bronze badges
Add a ment  | 

4 Answers 4

Reset to default 8

As per the React-Native, it is not ideal to use FlatList inside the ScrollView. What you can do instead is, to use nested scroll view. The only thing which is going to be changed is you have to do .map(), and populate the data inside the ScrollView inspite of renderItem from FlatList, and every thing will else will work just fine.

  • Use flexGrow: 1, inside the ScrollView, then no height is required.

Your Structure now should be:

<View style={{flex: 1}} >
    <ScollView style={{flexGrow: 1}} 
     nestedScrollEnabled={true}>
        <View>
            <SafeAreaView>
                <ScrollView horizontal 
                style={{width: '100%', height: 'your_height'}}>
                  {data.map(item, index) => (
                    <View key={index}>
                      // Your ponent
                    </View>
                  )}
                </ScrollView> -- Horizontal Scroll --
            </SafeAreaView>
        </View>
        <View>
        </View>
        <View>
            <ScrollView horizontal 
            style={{width: '100%', height: 'your_height'}}>
                  {anotherData.map(item, index) => (
                    <View key={index}>
                      // Your ponent
                    </View>
                  )}
            </ScrollView> -- Horizontal Scroll --
        </View>
        <View>
            <ScrollView horizontal
            style={{width: '100%', height: 'your_height'}}>
                  {newData.map(item, index) => (
                    <View key={index}>
                      // Your ponent
                    </View>
                  )}
            </ScrollView> -- Horizontal Scroll --      
        </View>
    </ScrollView>
</View>

as both flatlist and scrollView have vertical direction thats why it is giving error . what you need to do is that encolose your flatlist in another scrollview and make its direction horizontal like that

<ScrollView horizontal={true} contentContinaerStyle={{flexGrow: 1}}>
    <FlatList>
</ScrollView>

here by using flexGrow 1 the scrollView will takeup full width

Adding scrollEnabled={false} directive to the FlatList resolved the issue for me in React Native 0.72.3.

Instead of using FlatList, I rendered the list with the help of map

  <ScrollView>
    other ponents
    {
        list.map((item) => <ListItem key={item.id} item={item}/>)
    }
    other ponents
  </ScrollView>

This solution works great if you have a small list to render but larger list will give you performance and memory issues

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论