In this lesson, you will learn how to manually reconstruct complex grid-based layouts using React Native’s flexbox model and basic arithmetic.
While CSS Grid allows you to define rows and columns declaratively, React Native relies on Flexbox. To achieve a "Grid" look, we have to treat containers as rows and children as elements with specific percentage-based widths.
The Math of Grids
When building a grid in React Native, the formula is simple: Width = 100% / number of columns. If you want a 3-column grid with a 10px gap, you cannot simply use gap (which is newer and inconsistent across older native versions). Instead, we use flexWrap: 'wrap' on a parent container and calculate the width of each child.
const GridExample = () => (
<View style={{ flexDirection: 'row', flexWrap: 'wrap' }}>
{[1, 2, 3, 4, 5, 6].map((i) => (
<View
key={i}
style={{
width: '33.33%', // Math: 100% / 3 columns
aspectRatio: 1, // Keeps it a perfect square
padding: 5
}}
>
<View style={{ flex: 1, backgroundColor: 'skyblue' }} />
</View>
))}
</View>
);
Why Manual Math Matters
In CSS Grid, you define the container layout. In React Native, the child dictates the space. By setting flexWrap: 'wrap' on the parent, you instruct the layout engine to push elements to a new line once the parent's width is exhausted.
To mimic the "grid-gap," we apply padding to the children. Because padding is additive, if you want perfectly aligned edges, you often need to apply a negative margin to the parent container to offset the outer padding of the first and last items—a common trick in high-performance layouts to ensure everything lines up visually without triggering extra layout passes.
Remember: React Native’s layout engine is strictly linear. When you see a grid, think of it as a series of rows that have been "wrapped" into a container. By mastering the relationship between flexWrap, percentage widths, and aspectRatio, you can replicate any rigid CSS Grid design without needing external layout libraries.
Key Takeaway: Since React Native lacks a native Grid system, reconstruct grids by setting flexWrap: 'wrap' on a parent view and using percentage-based widths on children to force them into columns.