Created
January 7, 2022 06:18
-
-
Save intergalacticspacehighway/b396cb48464222715b807d5b13dfe5a8 to your computer and use it in GitHub Desktop.
Add row/col gaps in flatlist
This file contains hidden or 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
function App() { | |
const data = Array(10).fill(0); | |
const GAP = 5; | |
const numColumns = 3; | |
const { width } = Dimensions.get("window"); | |
// Reduce the size to accomodate margin space by items | |
const ITEM_SIZE = width / numColumns - ((numColumns - 1) * GAP) / numColumns; | |
const renderItem = ({ index }) => { | |
const col = index % numColumns; | |
const isInFirstCol = col === 0; | |
return ( | |
<View | |
style={ | |
{ | |
marginLeft: isInFirstCol ? 0 : GAP, | |
} | |
} | |
> | |
<View | |
style={{ | |
width: ITEM_SIZE, | |
height: ITEM_SIZE, | |
backgroundColor: "black", | |
}} | |
/> | |
</View> | |
); | |
}; | |
return ( | |
<FlatList | |
renderItem={renderItem} | |
data={data} | |
numColumns={numColumns} | |
keyExtractor={(_item, index) => index.toString()} | |
ItemSeparatorComponent={() => ( | |
<View style={{ height: GAP }} /> | |
)} | |
key={numColumns} | |
/> | |
); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment