无法循环和渲染.map函数内的FlatList react-native

时间:2019-03-25 07:01:41

标签: javascript react-native react-native-flatlist

我正在尝试在组件内部渲染FlatList。组件本身位于ScrollView中。

我正在使用map函数遍历数据以传递到组件中。 之前我使用的是ScrollView而不是FlatList。它工作正常,但渲染速度很慢。所以我决定使用FlatList。

这是我的代码:

    renderComp(){

        const { filtersView,cats,cats_title, clearStyle} = styles;

        const data = this.props.ingreds;

        const arr  = Object.entries(data);


        return arr.map(i=> { 
              const name= i[0]; 
              const items_obj = i[1];
              const items = Object.values(items_obj);

              return(

                <View key={name} style= {filtersView}>

                    <View style={cats}>

                      <Text  style ={cats_title}>{name}</Text>

                      <Text style={clearStyle}>Clear All</Text>
                    </View>


                    <View style={{justifyContent:'flex-start', alignItems:'flex-start'}}>

                      <FlatList 
                      style={{ marginRight:6}}  
                      data={items}
                      keyExtractor={(x,i)=> i.toString()}
                      renderItem={({item}) =>{
                      this.renderItems(item)
                      }}
                      />
                    </View> 
                  </View>

              )
        })

      }

这是ScrollView组件:

        <ScrollView contentContainerStyle={{alignItems:'flex-start', 
        justifyContent:'flex-start',flex:1, height:72}} >

            {this.renderComp()}

        </ScrollView>

然后循环在一次迭代后停止。

以下是输出:https://i.stack.imgur.com/yM151.png

有什么建议吗?

1 个答案:

答案 0 :(得分:1)

ReactNative FlatList renderItem方法应返回一个?React.Element组件。在您的情况下,请使用return this.renderItems或跳过内括号。

https://facebook.github.io/react-native/docs/flatlist#renderitem

({item}) => this.renderItems(item)}