Stack
A flexible layout component for arranging elements vertically or horizontally with consistent spacing.
Stack
The Stack component provides a simple way to create consistently spaced vertical or horizontal layouts. It's ideal for arranging items in a sequence with uniform spacing between them.
Features
- Direction Control: Arrange items vertically or horizontally
- Consistent Spacing: Uniform gaps between elements
- Alignment Options: Control alignment along both axes
- Responsive: Works well in various screen sizes
- Nesting Support: Stack components can be nested for complex layouts
- Styling Control: Accepts additional Tailwind classes and style objects
Import
Props
Prop | Type | Default | Description |
---|---|---|---|
spacing | number | 4 | Spacing between items (in points) |
direction | 'vertical' | 'horizontal' | 'vertical' | Direction of the stack layout |
align | 'start' | 'center' | 'end' | 'stretch' | 'start' | Alignment of items perpendicular to the stack direction |
justify | 'start' | 'center' | 'end' | 'between' | 'around' | 'start' | Justification of items along the stack direction |
className | string | '' | Additional Tailwind classes |
style | ViewStyle | — | Additional style object |
children | React.ReactNode | Required | Content to display in the stack |
Usage Examples
Basic Vertical Stack
Horizontal Stack
Stack with Alignment
Nested Stacks
Form Layout Example
Card Layout Example
Best Practices
Choose Appropriate Direction
Select the direction based on your content:
Consistent Spacing
Use consistent spacing values throughout your application for a polished look:
Use with Other Layout Components
Combine Stack with other layout components for complex UIs:
Implementation Details
The Stack component uses React Native's View
component and applies Tailwind CSS classes for direction, alignment, and justification. The spacing between items is implemented using the gap
style property.
The component handles rendering null or undefined children by filtering them out, which ensures that the layout remains consistent even when some items are conditionally rendered.
The alignment and justification properties are mapped to the corresponding Tailwind CSS classes:
align
controlsitems-*
classesjustify
controlsjustify-*
classes
Notes
- The Stack component is ideal for creating consistent vertical or horizontal layouts
- For grid layouts, consider using the Grid component instead
- The spacing prop is measured in logical pixels rather than in a responsive unit
- Empty or null children are automatically filtered out to avoid layout issues
- The component works well on both iOS and Android