Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Wrap Flatlist items in React Native

I am trying to render the list of items like in this image. enter image description here

Items in each row will vary based on their text size. Flatlist is using for rendering items.

TagView.js

<View style={styles.tagView}>
    <FlatList 
        scrollEventThrottle={1900} 
        data={this.state.interests} 
        numColumns={5}
        renderItem={({ item }) => (
           <View style={styles.tag}>
               <Text>{item.tagName}</Text>
           </View>
        )}
        keyExtractor={(item, index) => index}
        contentContainerStyle={{ paddingBottom: 100 }}
    />
</View>

Style

tagView: {
    flex: 1,
    flexDirection: "row",
    flexWrap: "wrap"
},
tag: {
    borderWidth: 1,
    borderRadius: 10,
    borderColor: "black",
    backgroundColor: "white",
    padding: 3,
    marginTop: 5
}

Result

enter image description here

But here items are not wrapping with device width. Is there any to wrap the contents?

like image 416
Muhsin Keloth Avatar asked Aug 13 '18 10:08

Muhsin Keloth


3 Answers

FlatList with columnWrapperStyle

<FlatList 
    columnWrapperStyle={styles.tagView}
    scrollEventThrottle={1900} 
    data={this.state.interests} 
    numColumns={5}
    renderItem={({ item }) => (
       <View style={styles.tag}>
           <Text>{item.tagName}</Text>
       </View>
    )}
    keyExtractor={(item, index) => index}
    contentContainerStyle={{ paddingBottom: 100 }}
/>

Style

tagView: {
  flexWrap: "wrap"
},
like image 177
Hashim pk Avatar answered Oct 18 '22 22:10

Hashim pk


Add Horizontal Prop and try,

      <FlatList 
        scrollEventThrottle={1900} 
        data={this.state.interests} 
        numColumns={5}
        horizontal={false}
        renderItem={({ item }) => (
           <View style={styles.tag}>
               <Text>{item.tagName}</Text>
           </View>
        )}
        keyExtractor={(item, index) => index}
        contentContainerStyle={{ paddingBottom: 100 }}
    />
like image 28
Yogaraj Saravanan Avatar answered Oct 18 '22 20:10

Yogaraj Saravanan


Use ScrollView component for this purpose.

<ScrollView contentContainerStyle={{ flexDirection: 'row', flexWrap: 'wrap' }}>
  {tags.map(({ id, name }) => (
     <Text key={id}>{name}</Text>
  ))}
</ScrollView>
like image 1
Michael Avatar answered Oct 18 '22 21:10

Michael