Flexbox helps arrange items in a container easily. It makes layouts flexible and neat on different screen sizes.
Flexbox layout (flexDirection, justifyContent, alignItems) in React Native
const styles = StyleSheet.create({
container: {
flexDirection: 'row', // or 'column'
justifyContent: 'flex-start', // or 'center', 'flex-end', 'space-between', 'space-around', 'space-evenly'
alignItems: 'flex-start' // or 'center', 'flex-end', 'stretch', 'baseline'
}
});flexDirection sets the main axis direction: 'row' means left to right, 'column' means top to bottom.
justifyContent aligns items along the main axis (horizontal if row, vertical if column).
flexDirection: 'row' justifyContent: 'center' alignItems: 'center'
flexDirection: 'column' justifyContent: 'space-between' alignItems: 'flex-start'
flexDirection: 'row' justifyContent: 'space-around' alignItems: 'stretch'
This example shows three boxes arranged in a row. They have equal space around them and are centered vertically inside a light blue container.
import React from 'react'; import { View, Text, StyleSheet } from 'react-native'; export default function FlexboxExample() { return ( <View style={styles.container} accessibilityLabel="Flexbox container"> <Text style={styles.box} accessibilityLabel="Box 1">Box 1</Text> <Text style={styles.box} accessibilityLabel="Box 2">Box 2</Text> <Text style={styles.box} accessibilityLabel="Box 3">Box 3</Text> </View> ); } const styles = StyleSheet.create({ container: { flexDirection: 'row', justifyContent: 'space-around', alignItems: 'center', height: 100, backgroundColor: '#e0f7fa', paddingHorizontal: 20 }, box: { backgroundColor: '#00796b', color: 'white', padding: 10, fontSize: 16, borderRadius: 5 } });
Use flexDirection to switch between horizontal and vertical layouts.
justifyContent controls spacing along the main axis, while alignItems controls alignment on the cross axis.
Accessibility labels help screen readers describe the layout and items.
Flexbox makes arranging items easy and flexible on any screen.
flexDirection sets the main layout direction.
justifyContent and alignItems control spacing and alignment.
Practice
flexDirection property control in a React Native Flexbox layout?Solution
Step 1: Understand flexDirection role
flexDirectionsets whether children are laid out horizontally (row) or vertically (column).Step 2: Compare options
Only The main axis direction of child elements (row or column) describes this behavior correctly. Other options relate to unrelated styles.Final Answer:
The main axis direction of child elements (row or column) -> Option CQuick Check:
flexDirection controls layout direction [OK]
- Confusing flexDirection with justifyContent or alignItems
- Thinking flexDirection changes colors or fonts
- Assuming flexDirection controls spacing
Solution
Step 1: Identify correct property for layout direction
The property to set layout direction isflexDirection.Step 2: Check correct value for vertical layout
To arrange children vertically, use value 'column'.style={{flexDirection: 'column'}}matches this exactly.Final Answer:
style={{flexDirection: 'column'}} -> Option BQuick Check:
flexDirection: 'column' sets vertical layout [OK]
- Using justifyContent or alignItems to set direction
- Using invalid values like 'column' for justifyContent
- Mixing quotes or syntax errors in style object
<View style={{flexDirection: 'row', justifyContent: 'center', alignItems: 'flex-start'}}>
<View style={{width: 50, height: 50, backgroundColor: 'red'}} />
<View style={{width: 50, height: 50, backgroundColor: 'blue'}} />
</View>Solution
Step 1: Analyze flexDirection and justifyContent
flexDirection: 'row' means children are laid out horizontally. justifyContent: 'center' centers children along this horizontal main axis.Step 2: Analyze alignItems
alignItems: 'flex-start' aligns children at the top along the vertical cross axis.Final Answer:
Children are centered horizontally and aligned at the top vertically -> Option DQuick Check:
Row + justifyContent center + alignItems flex-start = Children are centered horizontally and aligned at the top vertically [OK]
- Mixing up justifyContent and alignItems axes
- Assuming 'flex-start' aligns horizontally in row direction
- Ignoring flexDirection effect on axes
<View style={{flexDirection: 'column', justifyContent: 'left', alignItems: 'center'}}>
<Text>Hello</Text>
</View>Solution
Step 1: Check valid values for justifyContent
justifyContent accepts values like 'flex-start', 'center', 'flex-end', not 'left'. 'left' is invalid and causes error.Step 2: Correct the invalid value
Replace 'left' with 'flex-start' to align children at the start of the main axis.Final Answer:
Change justifyContent: 'left' to justifyContent: 'flex-start' -> Option AQuick Check:
Use 'flex-start' instead of 'left' for justifyContent [OK]
- Using 'left' or 'right' instead of flex-start/flex-end
- Confusing justifyContent with alignItems values
- Removing needed alignment properties
Solution
Step 1: Set vertical stacking
UseflexDirection: 'column'to stack boxes vertically.Step 2: Space boxes evenly with equal space around
justifyContent: 'space-around'places equal space above, between, and below children.Step 3: Center boxes horizontally
alignItems: 'center'centers children along the horizontal cross axis.Final Answer:
{flexDirection: 'column', justifyContent: 'space-around', alignItems: 'center'} -> Option AQuick Check:
Column + space-around + center aligns as required [OK]
- Using flexDirection: 'row' for vertical stacking
- Using space-between which lacks space above first and below last
- Misaligning boxes horizontally with wrong alignItems
Ready to prove it?
Pass the test and this pattern moves unknown
