Dynamic number of columns in flatlist

44 views Asked by At

In react native , I need to create a list with a number of items, but the texts are of different lengths. So, I can't determine how many items can be included in a single row. I need to calculate how many items can be included in a row and display it. As shown below on the 7th line, it can only include 2 items, so I need to display the 3rd item in the next row. How can I do that?

7 th line

2

There are 2 answers

0
fire app On

You can try to create the list using map function:

<View style={{ flexDirection: "row", flexWrap: "wrap" }}>
  {Data?.map((i, j) => <YourComponent key={j} />)}
</View>
0
J.dev On

If you use a FlatList you can use the prop contentContainerStyle to achieve it by adding a flexDirection to "row" and a flexWrap to "wrap".

<FlatList
  contentContainerStyle={{flexDirection : "row", flexWrap : "wrap"}}
  data={...}
  renderItem{...}
/>