Forked from intergalacticspacehighway/FlatListGapExample.jsx
Created
December 3, 2024 10:57
-
-
Save issam-seghir/66b590897dd0b38b6f8d7fa49ea8cc91 to your computer and use it in GitHub Desktop.
Add gap in FlatList items with numColumns
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import {Dimensions, FlatList, View} from 'react-native'; | |
const screenWidth = Dimensions.get('window').width; | |
const numColumns = 2; | |
const gap = 5; | |
const availableSpace = screenWidth - (numColumns - 1) * gap; | |
const itemSize = availableSpace / numColumns; | |
const renderItem = ({item}) => { | |
return ( | |
<View | |
style={{ | |
height: itemSize, | |
width: itemSize, | |
backgroundColor: 'pink', | |
}} | |
/> | |
); | |
}; | |
const RNTesterApp = props => { | |
return ( | |
<FlatList | |
renderItem={renderItem} | |
data={new Array(100).fill(0)} | |
numColumns={numColumns} | |
contentContainerStyle={{gap}} | |
columnWrapperStyle={{gap}} | |
key={numColumns} | |
/> | |
); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment