Learn React Native components, Flexbox layout, and handling safe areas on mobile devices.
Core Components
React Native doesn't use HTML elements. Instead, it provides native-backed components:
| Web | React Native | Purpose |
|---|---|---|
<div> | <View> | Container, layout wrapper |
<span>/<p> | <Text> | Text content (required for all text) |
<img> | <Image> | Images (local or remote) |
<ul>/<ol> | <FlatList>/<SectionList> | Performant lists |
<input> | <TextInput> | User input |
<button> | <Pressable>/<Button> | Touch interactions |
<a> | <Link> (Expo Router) | Navigation |
View - The Building Block
import { View, Text, StyleSheet } from 'react-native';
export default function ComponentLayout() {
return (
<View style={styles.container}>
<View style={styles.header}>
<Text style={styles.title}>My App</Text>
</View>
<View style={styles.content}>
<Text>Content goes here</Text>
</View>
<View style={styles.footer}>
<Text>Footer</Text>
</View>
</View>
);
}
const styles = StyleSheet.create({
container: { flex: 1 }, // Full screen
header: { height: 60, backgroundColor: '#3b82f6' },
content: { flex: 1, padding: 16 }, // Grows to fill space
footer: { height: 50, backgroundColor: '#f3f4f6' },
title: { color: 'white', fontSize: 20, fontWeight: '600' },
});Text - All Text Must Be in Text
// ❌ Wrong - Text outside Text component
<View>Hello World</View>
// ✅ Correct
<Text>Hello World</Text>
// ✅ Nested text with different styles
<Text>
<Text style={{ fontWeight: 'bold' }}>Bold </Text>
<Text style={{ color: 'blue' }}>Blue</Text>
</Text>
// Number must be converted to string
<Text>{String(42)}</Text>
<Text>{42.toString()}</Text>Image - Local and Remote
import { Image, View, StyleSheet } from 'react-native';
export default function ImageExamples() {
return (
<View style={styles.container}>
{/* Local image (bundled with app) */}
<Image
source={require('../assets/logo.png')}
style={styles.logo}
resizeMode="contain"
/>
{/* Remote image */}
<Image
source={{ uri: 'https://example.com/image.jpg' }}
style={styles.remote}
resizeMode="cover"
/>
{/* With loading/error states */}
<Image
source={{ uri: 'https://example.com/large.jpg' }}
style={styles.remote}
onLoadStart={() => console.log('Loading...')}
onLoad={() => console.log('Loaded!')}
onError={() => console.log('Failed!')}
/>
</View>
);
}ScrollView vs FlatList
import { ScrollView, FlatList, View, Text, StyleSheet } from 'react-native';
const items = Array.from({ length: 100 }, (_, i) => `Item ${i + 1}`);
// ❌ ScrollView - renders ALL items at once (bad for long lists)
export function BadList() {
return (
<ScrollView>
{items.map((item, index) => (
<Text key={index} style={styles.item}>{item}</Text>
))}
</ScrollView>
);
}
// ✅ FlatList - virtualized, only renders visible items
export function GoodList() {
return (
<FlatList
data={items}
keyExtractor={(item, index) => index.toString()}
renderItem={({ item }) => <Text style={styles.item}>{item}</Text>}
// Performance optimizations
getItemLayout={(data, index) => ({ length: 50, offset: 50 * index, index })}
removeClippedSubviews={true}
maxToRenderPerBatch={10}
windowSize={10}
/>
);
}
const styles = StyleSheet.create({
item: { padding: 16, borderBottomWidth: 1, borderBottomColor: '#e5e7eb' },
});Flexbox Layout
React Native uses Flexbox with some differences from web:
Defaults Are Different
| Property | Web Default | React Native Default |
|---|---|---|
flexDirection | row | column |
alignItems | stretch | stretch |
justifyContent | flex-start | flex-start |
// Column layout (default) - stacks vertically
<View style={{ flex: 1 }}>
<View style={{ height: 100, backgroundColor: 'red' }} />
<View style={{ height: 100, backgroundColor: 'blue' }} />
<View style={{ height: 100, backgroundColor: 'green' }} />
</View>
// Row layout - side by side
<View style={{ flex: 1, flexDirection: 'row' }}>
<View style={{ flex: 1, backgroundColor: 'red' }} />
<View style={{ flex: 1, backgroundColor: 'blue' }} />
<View style={{ flex: 1, backgroundColor: 'green' }} />
</View>Flex Values
<View style={{ flexDirection: 'row' }}>
{/* flex: 1 = grow to fill remaining space equally */}
<View style={{ flex: 1, backgroundColor: 'red' }} />
<View style={{ flex: 2, backgroundColor: 'blue' }} /> {/* 2x width */}
<View style={{ flex: 1, backgroundColor: 'green' }} />
{/* flex: 0 = don't grow, use content size */}
<View style={{ flex: 0, width: 50, backgroundColor: 'yellow' }} />
{/* flex: -1 = shrink if needed (rare) */}
</View>Alignment
// justifyContent: main axis (column = vertical, row = horizontal)
<View style={{ flex: 1, justifyContent: 'center' }}> {/* Center vertically */}
<View style={{ flex: 1, justifyContent: 'space-between' }}> {/* Distribute */}
<View style={{ flex: 1, justifyContent: 'space-around' }}> {/* Equal space */}
<View style={{ flex: 1, justifyContent: 'flex-end' }}> {/* Bottom/right */}
// alignItems: cross axis
<View style={{ flex: 1, flexDirection: 'row', alignItems: 'center' }}> {/* Center vertically in row */}
<View style={{ flex: 1, flexDirection: 'row', alignItems: 'stretch' }}> {/* Stretch to match tallest */}
<View style={{ flex: 1, flexDirection: 'row', alignItems: 'flex-start' }}> {/* Top align */}
<View style={{ flex: 1, flexDirection: 'row', alignItems: 'flex-end' }}> {/* Bottom align */}
// alignSelf: override for single child
<View style={{ flex: 1, flexDirection: 'row' }}>
<View style={{ alignSelf: 'center', height: 50 }} /> {/* Center this one */}
<View style={{ alignSelf: 'flex-end', height: 50 }} /> {/* Bottom this one */}
</View>Centering (The Classic Problem)
// Perfect center - both axes
<View style={{ flex: 1, justifyContent: 'center', alignItems: 'center' }}>
<Text>Perfectly Centered!</Text>
</View>
// Center in row
<View style={{ flex: 1, flexDirection: 'row', justifyContent: 'center', alignItems: 'center' }}>
<Text>Centered in Row</Text>
</View>Common Layout Patterns
Card Layout
import { View, Text, Image, StyleSheet, Pressable } from 'react-native';
export function MovieCard({ title, year, poster, onPress }: MovieCardProps) {
return (
<Pressable onPress={onPress} style={styles.card}>
<Image source={{ uri: poster }} style={styles.poster} resizeMode="cover" />
<View style={styles.content}>
<Text style={styles.title}>{title}</Text>
<Text style={styles.year}>{year}</Text>
</View>
</Pressable>
);
}
const styles = StyleSheet.create({
card: {
flexDirection: 'row',
backgroundColor: 'white',
borderRadius: 12,
overflow: 'hidden',
marginBottom: 12,
elevation: 3,
shadowColor: '#000',
shadowOffset: { width: 0, height: 2 },
shadowOpacity: 0.1,
shadowRadius: 4,
},
poster: { width: 80, height: 120 },
content: { flex: 1, padding: 12, justifyContent: 'center' },
title: { fontSize: 16, fontWeight: '600' },
year: { fontSize: 14, color: '#6b7280', marginTop: 4 },
});Form Layout
import { View, TextInput, Text, StyleSheet, KeyboardAvoidingView, Platform, ScrollView } from 'react-native';
export function LoginForm() {
return (
<KeyboardAvoidingView
behavior={Platform.OS === 'ios' ? 'padding' : 'height'}
style={styles.container}
keyboardVerticalOffset={Platform.OS === 'ios' ? 0 : 0}
>
<ScrollView contentContainerStyle={styles.scrollContent} keyboardShouldPersistTaps="handled">
<Text style={styles.title}>Sign In</Text>
<View style={styles.field}>
<Text style={styles.label}>Email</Text>
<TextInput
style={styles.input}
placeholder="you@example.com"
keyboardType="email-address"
autoCapitalize="none"
textContentType="emailAddress"
/>
</View>
<View style={styles.field}>
<Text style={styles.label}>Password</Text>
<TextInput
style={styles.input}
placeholder="••••••••"
secureTextEntry
textContentType="password"
/>
</View>
<Pressable style={styles.button} onPress={() => {}}>
<Text style={styles.buttonText}>Sign In</Text>
</Pressable>
</ScrollView>
</KeyboardAvoidingView>
);
}
const styles = StyleSheet.create({
container: { flex: 1, backgroundColor: '#f9fafb' },
scrollContent: { flexGrow: 1, padding: 24, justifyContent: 'center' },
title: { fontSize: 28, fontWeight: '700', marginBottom: 32, textAlign: 'center' },
field: { marginBottom: 16 },
label: { fontSize: 14, fontWeight: '500', marginBottom: 8, color: '#374151' },
input: {
backgroundColor: 'white',
borderWidth: 1,
borderColor: '#d1d5db',
borderRadius: 8,
padding: 16,
fontSize: 16,
},
button: {
backgroundColor: '#3b82f6',
borderRadius: 8,
padding: 16,
alignItems: 'center',
marginTop: 8,
},
buttonText: { color: 'white', fontSize: 16, fontWeight: '600' },
});Safe Areas
Mobile devices have notches, home indicators, and status bars. Use SafeAreaView:
import { SafeAreaView, View, Text, StyleSheet, StatusBar } from 'react-native';
// Basic safe area
export default function App() {
return (
<SafeAreaView style={styles.container}>
<StatusBar barStyle="dark-content" backgroundColor="#fff" />
<View style={styles.content}>
<Text>Content respects safe areas</Text>
</View>
</SafeAreaView>
);
}
// With edges (customize which edges to avoid)
import { SafeAreaView, useSafeAreaInsets } from 'react-native-safe-area-context';
export function CustomSafeArea() {
const insets = useSafeAreaInsets();
return (
<View style={{ flex: 1, paddingTop: insets.top, paddingBottom: insets.bottom }}>
<Text>Top inset: {insets.top}, Bottom inset: {insets.bottom}</Text>
</View>
);
}Common Safe Area Patterns
// Screen with header that extends behind status bar
<SafeAreaView style={styles.container} edges={['top', 'horizontal']}>
<View style={styles.header}> // Header can go behind status bar
<Text>My App</Text>
</View>
<SafeAreaView style={styles.content} edges={['bottom', 'horizontal']}>
{/* Content respects bottom home indicator */}
</SafeAreaView>
</SafeAreaView>
// Modal that avoids all edges
<SafeAreaView style={styles.modal} edges={['top', 'bottom', 'horizontal']}>
<View style={styles.modalContent}>Modal Content</View>
</SafeAreaView>Keyboard Handling
import { KeyboardAvoidingView, ScrollView, Platform, TextInput, View, StyleSheet } from 'react-native';
export function FormWithKeyboard() {
return (
<KeyboardAvoidingView
behavior={Platform.OS === 'ios' ? 'padding' : 'height'}
style={styles.container}
keyboardVerticalOffset={0}
>
<ScrollView
contentContainerStyle={styles.scrollContent}
keyboardShouldPersistTaps="handled"
showsVerticalScrollIndicator={false}
>
{/* Form fields */}
<TextInput style={styles.input} placeholder="Field 1" />
<TextInput style={styles.input} placeholder="Field 2" />
<TextInput style={styles.input} placeholder="Field 3" />
{/* ... more fields */}
</ScrollView>
</KeyboardAvoidingView>
);
}
// Alternative: react-native-keyboard-controller (more advanced)
import { KeyboardController, KeyboardAwareScrollView } from 'react-native-keyboard-controller';
export function BetterForm() {
return (
<KeyboardController>
<KeyboardAwareScrollView>
{/* Form fields automatically scroll into view */}
</KeyboardAwareScrollView>
</KeyboardController>
);
}Platform-Specific Styles
import { Platform, StyleSheet, View, Text } from 'react-native';
const styles = StyleSheet.create({
container: {
paddingTop: Platform.OS === 'ios' ? 44 : 24, // iOS status bar height
backgroundColor: Platform.select({ ios: '#fff', android: '#fafafa' }),
},
shadow: {
...Platform.select({
ios: {
shadowColor: '#000',
shadowOffset: { width: 0, height: 2 },
shadowOpacity: 0.1,
shadowRadius: 4,
},
android: {
elevation: 4,
},
}),
},
});
// Platform-specific file extensions
// MyComponent.ios.tsx - iOS only
// MyComponent.android.tsx - Android only
// MyComponent.native.tsx - Both mobile (not web)
// MyComponent.web.tsx - Web onlyDimensions and Pixel Ratio
import { Dimensions, PixelRatio, View, StyleSheet } from 'react-native';
const { width: SCREEN_WIDTH, height: SCREEN_HEIGHT } = Dimensions.get('window');
const scale = PixelRatio.getFontScale();
// Responsive width
const styles = StyleSheet.create({
container: { width: SCREEN_WIDTH * 0.9 },
halfScreen: { width: SCREEN_WIDTH / 2 },
fontSize: { fontSize: 16 * scale }, // Respects user's font size setting
});
// Listen for dimension changes (rotation, foldables)
import { useWindowDimensions } from 'react-native';
export function ResponsiveComponent() {
const { width, height, fontScale } = useWindowDimensions();
return (
<View style={{ flex: 1 }}>
<Text>Width: {width}, Height: {height}, Font Scale: {fontScale}</Text>
</View>
);
}Try It
Experiment with flexDirection, justifyContent, alignItems. Try nesting flex containers.
import { View, Text, StyleSheet } from 'react-native';
export default function FlexPlayground() {
return (
<View style={styles.container}>
<View style={styles.box1}><Text>1</Text></View>
<View style={styles.box2}><Text>2</Text></View>
<View style={styles.box3}><Text>3</Text></View>
</View>
);
}
const styles = StyleSheet.create({
container: { flex: 1, flexDirection: 'row', justifyContent: 'space-around', alignItems: 'center' },
box1: { width: 80, height: 80, backgroundColor: '#ef4444', justifyContent: 'center', alignItems: 'center' },
box2: { width: 80, height: 80, backgroundColor: '#3b82f6', justifyContent: 'center', alignItems: 'center' },
box3: { width: 80, height: 80, backgroundColor: '#22c55e', justifyContent: 'center', alignItems: 'center' },
});Build a responsive profile screen
- Create a profile screen with avatar, name, bio, and stats row
- Use Flexbox for all layout (no absolute positioning)
- Handle safe areas for notches and home indicator
- Make it work on phone and tablet (use Dimensions)
- Add KeyboardAvoidingView for edit mode
React Native's Flexbox defaults to column direction. Master flex: 1 for growing, justifyContent/alignItems for alignment, and always wrap screens in SafeAreaView. Use FlatList for lists, never ScrollView with .map().
Resources
- Expo Router Introduction: https://docs.expo.dev/router/introduction/
- React Native Core Components and APIs: https://reactnative.dev/docs/components-and-apis
- React Native Flexbox: https://reactnative.dev/docs/flexbox
- Safe Area Context: https://github.com/th3rdwave/react-native-safe-area-context