Grid
Use the Grid component to list and layout your items in a single or multi-column grid.
Props
columns
Use this to set the number of columns.
Type | Default | Description |
---|---|---|
number | 2 | Any positive number. |
Example
xGap
Use this to set horizontal gap between columns.
Type | Default | Description |
---|---|---|
number | 0 | Does not accept negative numbers. |
Example
yGap
Use this to set vertical gap between rows.
Type | Default | Description |
---|---|---|
number | 0 | Does not accept negative numbers. |
Example
children
Use this to set ReactNode items.
Type | Default | Description |
---|---|---|
ReactNode | / | Items will get the width of the columns but you need to define the height on item level. |
Example
Preview
numOfItems
Use this to set the number of items to display.
Tips
Overflow hidden
It is there just in case to avoid unwanted behaviour in some cases (like horizontal/vertical scrolling in some situations). If you have problems having some elements cut off on the edges, you can remove that property if you are sure it won't cause any other unwanted behaviour.
Editor
Source code
import React from 'react';
import {View} from 'react-native';
const UIGrid = ({
columns = 2,
xGap = 0,
yGap = 0,
children,
}) => {
const styles = {
grid: {
overflow: 'hidden',
flexShrink: 0,
},
gridContent: {
flexShrink: 0,
flexDirection: 'row',
flexWrap: 'wrap',
marginHorizontal: -xGap / 2,
marginVertical: -yGap / 2,
},
gridItem: {
paddingHorizontal: xGap / 2,
paddingVertical: yGap / 2,
minWidth: 0,
minHeight: 0,
flexShrink: 0,
width: `${100 / columns}%`,
},
};
return (
<View style={styles.grid}>
<View style={styles.gridContent}>
{React.Children.map(children, (child) => {
return (
<View style={styles.gridItem}>{React.cloneElement(child)}</View>
);
})}
</View>
</View>
);
};
export default UIGrid;
Usage example
<UIGrid
xGap={12}
yGap={12}
>
<View style={{borderRadius: 16, height: 100, backgroundColor: '#1bbeb0'}}/>
<View style={{borderRadius: 16, height: 100, backgroundColor: '#1bbeb0'}}/>
<View style={{borderRadius: 16, height: 100, backgroundColor: '#1bbeb0'}}/>
<View style={{borderRadius: 16, height: 100, backgroundColor: '#1bbeb0'}}/>
<View style={{borderRadius: 16, height: 100, backgroundColor: '#1bbeb0'}}/>
<View style={{borderRadius: 16, height: 100, backgroundColor: '#1bbeb0'}}/>
</UIGrid>