Beginner-Friendly Topic
Take your time - it's perfectly normal to re-read this topic 2-3 times. Try the interactive code editor below to run code yourself. Use the Q&A section to check your understanding before moving on.You've got this!
Lesson 7 of 10
Flexbox in React Native
React Native uses Flexbox for all layout, powered by the Yoga engine (C++ implementation of the Flexbox spec). It is nearly identical to CSS Flexbox but with three important differences that trip up every web developer.
Key Differences from Web CSS Flexbox
- `flexDirection` defaults to `'column'` (not `'row'` like web) - this is the most common gotcha
- `flex` works like CSS `flex-grow` only - you cannot do `flex: 0 1 auto`; just `flex: 1` means 'take all remaining space'
- `StyleSheet` values are unitless numbers (density-independent pixels, dp) - `fontSize: 16` means 16dp, not 16px
- No `margin: auto` in RN (added in Yoga 3 for New Architecture) - use `alignSelf: 'center'` or explicit flex instead
- `overflow: 'hidden'` is required to clip children outside their parent bounds (does not clip on Android by default without it)
Flexbox Patterns
import { View, Text, StyleSheet } from 'react-native';
// ---- Center everything ----
const centered: ViewStyle = {
flex: 1,
justifyContent: 'center', // main axis (column → vertical)
alignItems: 'center', // cross axis (column → horizontal)
};
// ---- Row layout with gap ----
const row: ViewStyle = {
flexDirection: 'row',
alignItems: 'center',
gap: 12, // gap works in RN 0.71+ / Expo SDK 48+
};
// ---- Header + scrollable content + footer ----
function ScreenLayout({ children }: { children: React.ReactNode }) {
return (
<View style={{ flex: 1, backgroundColor: '#0f172a' }}>
{/* Header - fixed height */}
<View style={{ height: 56, backgroundColor: '#1e293b', justifyContent: 'center', paddingHorizontal: 16 }}>
<Text style={{ color: 'white', fontWeight: '700', fontSize: 18 }}>App Header</Text>
</View>
{/* Content - takes all remaining space */}
<View style={{ flex: 1 }}>
{children}
</View>
{/* Footer - fixed height */}
<View style={{ height: 60, backgroundColor: '#1e293b' }} />
</View>
);
}
// ---- Card grid (2 columns) ----
function CardGrid() {
return (
<View style={{ flexDirection: 'row', flexWrap: 'wrap', gap: 12, padding: 16 }}>
{items.map(item => (
// Each card = 50% minus half the gap
<View key={item.id} style={{ width: '47%', backgroundColor: '#1e293b', borderRadius: 10, padding: 12 }}>
<Text>{item.title}</Text>
</View>
))}
</View>
);
}Tip
Tip
Practice Flexbox in React Native in small, isolated examples before integrating into larger projects. Breaking concepts into small experiments builds genuine understanding faster than reading alone.
Mobile Flexbox behavior with column default direction and alignment rules
Practice Task
Note
Practice Task - (1) Write a working example of Flexbox in React Native from scratch without looking at notes. (2) Modify it to handle an edge case (empty input, null value, or error state). (3) Share your solution in the Priygop community for feedback.
Quick Quiz
Common Mistake
Warning
A common mistake with Flexbox in React Native is skipping edge case testing - empty inputs, null values, and unexpected data types. Always validate boundary conditions to write robust, production-ready react native code.
Key Takeaways
- React Native uses Flexbox for all layout, powered by the Yoga engine (C++ implementation of the Flexbox spec).
- `flexDirection` defaults to `'column'` (not `'row'` like web) - this is the most common gotcha
- `flex` works like CSS `flex-grow` only - you cannot do `flex: 0 1 auto`; just `flex: 1` means 'take all remaining space'
- `StyleSheet` values are unitless numbers (density-independent pixels, dp) - `fontSize: 16` means 16dp, not 16px
Finished this lesson?