Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React Native horizontal FlatList with multiple rows

Tags:

I'm trying to implement a horizontal scrolling list that has two rows. Using FlatList, the vertical scrolling list involves setting numColumns but there is no equivalent for using rows with horizontal.

I was successfully able to make it render properly, and it works flawlessly. However, a warning gets thrown saying setting flexWrap is not supported in VirtualizedList or FlatList, and to use numColumns. I cannot use numColumns as that is not meant for horizontal lists.

<FlatList     horizontal={true}     contentContainerStyle={{         flexDirection: 'column',         flexWrap: 'wrap'     }}     {...otherProps} /> 

I found the commit where this warning was added, but cannot find the reasoning behind it. There seems to be no way to make this work without a warning being thrown, at least without ditching FlatList entirely. Is there a more appropriate solution for horizontal lists with rows?

References:

  • Commit adding warning: https://github.com/facebook/react-native/commit/eac399b
  • FlatList Documentation: https://facebook.github.io/react-native/docs/flatlist.html
like image 271
Kenny Avatar asked Aug 29 '17 12:08

Kenny


People also ask

How do I use extra data on FlatList?

flatlist-simpleBy passing extraData={selectedId} to FlatList we make sure FlatList itself will re-render when the state changes. Without setting this prop, FlatList would not know it needs to re-render any items because it is a PureComponent and the prop comparison will not show any changes.

What is keyExtractor in FlatList?

For custom key names such as userId in the example above, you'll use the keyExtractor prop. It extracts the unique key name and its value and tells the FlatList component to track the items based on that value.

Which is better ScrollView or FlatList?

As opposed to the ScrollView, the FlatList renders only those elements that are currently being displayed on the screen (default: 10 items). Thus, it does not have any impact on the performance of the application. So, it is preferable to use the FlatList Component to display a large list of data.


2 Answers

Please do not use horizontal={true}. For this case you should use numColumns equal to the length of data / 2, and add a <ScrollView> tag. Forcing the number of columns to be half the total will force the list to wrap to the next line.

<ScrollView>     <FlatList         contentContainerStyle={{alignSelf: 'flex-start'}}         numColumns={Math.ceil(listData.length / 2)}         showsVerticalScrollIndicator={false}         showsHorizontalScrollIndicator={false}         data={listData}         renderItem={({ item, index }) => {             //push your code         }}     /> </ScrollView> 

Update 1: edit listData.length / 2 -> Math.ceil(listData.length / 2) following Alex Aung comment. Thanks Alex

like image 135
tuanngocptn Avatar answered Sep 17 '22 06:09

tuanngocptn


enhancement the first answer, that's what I did:

const listData = props.data ?? []; const numColumns = Math.ceil(listData.length / 2);         <ScrollView           horizontal           showsVerticalScrollIndicator={false}           showsHorizontalScrollIndicator={false}           contentContainerStyle={{ paddingVertical: 20 }}>           <FlatList             scrollEnabled={false}             contentContainerStyle={{               alignSelf: 'flex-start',             }}             numColumns={numColumns}             showsVerticalScrollIndicator={false}             showsHorizontalScrollIndicator={false}             data={listData}             renderItem={renderItem}           />         </ScrollView>  
like image 39
Abdelhalim Ahmed Avatar answered Sep 19 '22 06:09

Abdelhalim Ahmed