Layout Components: Composing Structure
What you'll learn: How to build reusable layout components that arrange their children in predictable, consistent ways throughout your app.
The Core Idea
Layout components are wrapper components whose primary job is to handle positioning and spacing of their children. Instead of repeating the same CSS Grid or Flexbox patterns everywhere, you create components like <Stack>, <Grid>, or <Card> that encapsulate those layout rules.
Think of layout components like furniture organizers: a bookshelf doesn't care what books you put on it—it just provides consistent spacing and arrangement. Similarly, a <Stack> component doesn't care what components you nest inside—it just spaces them vertically with consistent gaps.
Building a Simple Stack Component
function Stack({ children, gap = '1rem' }) {
return (
<div style={{ display: 'flex', flexDirection: 'column', gap }}>
{children}
</div>
);
}
// Usage
function ProfileCard() {
return (
<Stack gap="0.5rem">
<img src="avatar.jpg" />
<h2>John Doe</h2>
<p>Software Developer</p>
</Stack>
);
}
The Stack component handles the layout logic once. Now every part of your app can stack elements vertically without duplicating flexbox CSS.
Why This Matters
Layout components let you:
- Standardize spacing across your entire app
- Avoid CSS repetition by centralizing layout patterns
- Compose complex layouts from simple, reusable pieces
- Change layout globally by editing one component
Instead of writing display: flex in fifty places, you write <Stack> fifty times and control the behavior in one location.
Key Takeaway: Layout components encapsulate positioning and spacing logic, letting you compose consistent structure by wrapping content in reusable, purpose-built containers.