React-Native another VirtualizedList-backed container

后端 未结 15 777
名媛妹妹
名媛妹妹 2020-12-08 13:00

After upgrading to react-native 0.61 i get a lot of warnings like that:

VirtualizedLists should never be nested inside plain ScrollViews with the same orient         


        
相关标签:
15条回答
  • 2020-12-08 13:33

    Looking at the examples in docs I've changed container from:

    <ScrollView>
        <FlatList ... />
    </ScrollView>
    

    to:

    <SafeAreaView style={{flex: 1}}>
        <FlatList ... />
    </SafeAreaView>
    

    and all those warnings disappeared.

    0 讨论(0)
  • 2020-12-08 13:37

    Just in case this helps someone, this is how I fixed the error in my case.

    I had a FlatList nested inside a ScrollView:

    render() {
        return (
            <ScrollView>
                <Text>{'My Title'}</Text>
                <FlatList
                    data={this.state.myData}
                    renderItem={({ item }) => {
                        return <p>{item.name}</p>;
                    }}
                />
                {this.state.loading && <Text>{'Loading...'}</Text>}
            </ScrollView>
        );
    }
    

    and I got rid of the ScrollView by using the FlatList to render everything I needed, which got rid of the warning:

    render() {
        const getHeader = () => {
            return <Text>{'My Title'}</Text>;
        };
    
        const getFooter = () => {
            if (this.state.loading) {
                return null;
            }
            return <Text>{'Loading...'}</Text>;
        };
    
        return (
            <FlatList
                data={this.state.myData}
                renderItem={({ item }) => {
                    return <p>{item.name}</p>;
                }}
                ListHeaderComponent={getHeader}
                ListFooterComponent={getFooter}
            />
        );
    }
    
    0 讨论(0)
  • 2020-12-08 13:38

    As @Brahim stated above, setting the horizontal property to true seem to resolve the issues for a FlatList embedded in a ScrollView.

    0 讨论(0)
提交回复
热议问题