Explain it simply
Layout with Flexbox, in plain language
In React Native, every View's default flexDirection is 'column' (the opposite of web CSS's 'row' default) — child components are arranged top to bottom by default. Switching to flexDirection: 'row' arranges them side by side. justifyContent controls spacing along the main axis (flex-start, center, space-between), while alignItems controls alignment along the cross axis (flex-start, center, stretch).
Problem → Why → Solution
The problem — why this concept matters
Assuming web CSS's default flexDirection: row and getting the layout wrong in React Native (where the default is column)
A common mistake
Relying only on fixed width/height and ignoring flex: 1 — layout can break on devices with different screen sizes
Core concept — understand how it works
Treat Layout with Flexbox as a native UI contract covering Yoga constraints, pixel density, font scaling, safe areas, the keyboard, touch targets, the accessibility tree, and iOS/Android differences—not merely screen styling syntax.
- JavaScript state
- native component and layout
- Android or iOS UI
Explain the React and native-platform behavior of Layout with Flexbox
Core syntax or pattern
const styles = StyleSheet.create({
header: {
flexDirection: 'row',
justifyContent: 'space-between',
alignItems: 'center',
padding: 16,
},
content: {
flex: 1, // use the remaining space
justifyContent: 'center',
alignItems: 'center',
},
});Treat Layout with Flexbox as a native UI contract covering Yoga constraints, pixel density, font scaling, safe areas, the keyboard, touch targets, the accessibility tree, and iOS/Android differences—not merely screen styling syntax.
Read the code step by step
- JavaScript state
const styles = StyleSheet.create({In React Native, every View's default flexDirection is 'column' (the opposite of web CSS's 'row' default) — child components are arranged top to bottom by default. Switching to flexDirection: 'row' arranges them side by side. justifyContent controls spacing along the main axis (flex-start, center, space-between), while alignItems controls alignment along the cross axis (flex-start, center, stretch).
- native component and layout
},Treat Layout with Flexbox as a native UI contract covering Yoga constraints, pixel density, font scaling, safe areas, the keyboard, touch targets, the accessibility tree, and iOS/Android differences—not merely screen styling syntax.
- Android or iOS UI
});Test an original Layout with Flexbox example on Android and iOS with small and large screens, font scaling, dark mode, keyboard and screen reader use, slow/offline networking, denied permissions, and background/foreground transitions. Use DevTools, tests, and a release build where relevant.
- JavaScript state
- native component and layout
- Android or iOS UI
Success
Explain the React and native-platform behavior of Layout with Flexbox
Failure
Assuming web CSS's default flexDirection: row and getting the layout wrong in React Native (where the default is column)
Let's think about this for a moment
In React Native, every View's default flexDirection is 'column' (the opposite of web CSS's 'row' default) — child components are arranged top to bottom by default. Switching to flexDirection: 'row' arranges them side by side. justifyContent controls spacing along the main axis (flex-start, center, space-between), while alignItems controls alignment along the cross axis (flex-start, center, stretch).
Let's connect this to a real scenario
To build a header bar (logo on the left, menu icon on the right), just add flexDirection: 'row', justifyContent: 'space-between' to the View's style — the two children automatically get pushed apart. Adding flex: 1 to a child component's style makes it take up the available space — if you want responsive behavior across different screen sizes, using flex is better than fixed width/height.
Code Example
const styles = StyleSheet.create({
header: {
flexDirection: 'row',
justifyContent: 'space-between',
alignItems: 'center',
padding: 16,
},
content: {
flex: 1, // use the remaining space
justifyContent: 'center',
alignItems: 'center',
},
});You'll be able to build a header bar with flexDirection: row and arrange the logo/icon side by side.5-Minute Try-It
Put 3 boxes inside a View with flexDirection: 'row' and try justifyContent with 'flex-start', 'center', and 'space-between'.
A Quick Heads-Up
flex: 1 might not work if the parent container doesn't have a defined height (the root View needs flex: 1 too) — pay attention to the parent-child flex chain.
Real-world usage
How to apply it in a real project
Test an original Layout with Flexbox example on Android and iOS with small and large screens, font scaling, dark mode, keyboard and screen reader use, slow/offline networking, denied permissions, and background/foreground transitions. Use DevTools, tests, and a release build where relevant.
Mini challenge
Build it yourself
Test an original Layout with Flexbox example on Android and iOS with small and large screens, font scaling, dark mode, keyboard and screen reader use, slow/offline networking, denied permissions, and background/foreground transitions. Use DevTools, tests, and a release build where relevant.
- Rebuild the example without copying it
- Test a normal input and an edge case
- Predict the compiler or output result before running it
const styles = StyleSheet.create({
header: {
flexDirection: 'row',
justifyContent: 'space-between',
alignItems: 'center',
padding: 16,
},
content: {
flex: 1, // use the remaining space
justifyContent: 'center',
alignItems: 'center',
},
});Expected: Explain the React and native-platform behavior of Layout with Flexbox
Show hint
Test an original Layout with Flexbox example on Android and iOS with small and large screens, font scaling, dark mode, keyboard and screen reader use, slow/offline networking, denied permissions, and background/foreground transitions. Use DevTools, tests, and a release build where relevant.
Quick quiz 1 / 3
Quick quiz 2 / 3
Quick quiz 3 / 3
What to remember
- JavaScript state → native component and layout → Android or iOS UI
- Treat Layout with Flexbox as a native UI contract covering Yoga constraints, pixel density, font scaling, safe areas, the keyboard, touch targets, the accessibility tree, and iOS/Android differences—not merely screen styling syntax.
- Assuming web CSS's default flexDirection: row and getting the layout wrong in React Native (where the default is column)
- Test an original Layout with Flexbox example on Android and iOS with small and large screens, font scaling, dark mode, keyboard and screen reader use, slow/offline networking, denied permissions, and background/foreground transitions. Use DevTools, tests, and a release build where relevant.
React Native Documentation — React Native