Skip to content

Instantly share code, notes, and snippets.

@intergalacticspacehighway
Created February 28, 2023 01:14
Show Gist options
  • Star 11 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save intergalacticspacehighway/0def6d0b9b2672c3ae4b8ed5923a04b4 to your computer and use it in GitHub Desktop.
Save intergalacticspacehighway/0def6d0b9b2672c3ae4b8ed5923a04b4 to your computer and use it in GitHub Desktop.
Add gap in FlatList items with numColumns
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}
/>
);
};
@bryanltobing
Copy link

Thanks for this

@Kotpes
Copy link

Kotpes commented Oct 16, 2023

Exactly was I was looking for 🙏

@olegbigma
Copy link

It works! Thank You!

@rafaelrnzo
Copy link

thanks, its work

@imamrobani
Copy link

cool

@mhmd-zbib
Copy link

Legend, thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment