Saumay Paul
Saumay Paul

Reputation: 425

Unable to loop and render FlatList inside .map function react-native

I am trying to render a FlatList inside a component. The Component itself is inside a ScrollView.

I am using map function to loop through the data to pass into the component. Earlier I was using ScrollView instead of FlatList. It was working fine, but was rendering slow. So I decided to use FlatList.

Here's my code:

    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>

              )
        })

      }

And here's the ScrollView Component:

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

            {this.renderComp()}

        </ScrollView>

And The loop stops after one iteration.

Here's the output: https://i.sstatic.net/yM151.png

Any suggestions?

Upvotes: 1

Views: 2731

Answers (1)

jgoday
jgoday

Reputation: 2836

ReactNative FlatList renderItem method should return a ?React.Element component. In your case either use return this.renderItems or skip the inner brackets.

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

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

Upvotes: 1

Related Questions