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 2 of 10
Stack Navigator - Screens & Headers
Stack Navigator is the backbone of most apps - it manages a stack of screens with push, pop, and replace operations. Headers, back button behavior, and transition animations are all configurable.
Stack Navigator Setup
npm install @react-navigation/native-stackNative Stack Configuration
import { createNativeStackNavigator } from '@react-navigation/native-stack';
import type { NativeStackScreenProps } from '@react-navigation/native-stack';
// ---- Type the route params for full TypeScript support ----
export type RootStackParamList = {
Home: undefined; // no params
ProductDetail: { product: Product }; // with params
UserProfile: { userId: string; tab?: 'posts' | 'likes' };
Modal: undefined;
};
// Typed screen props
type HomeProps = NativeStackScreenProps<RootStackParamList, 'Home'>;
const Stack = createNativeStackNavigator<RootStackParamList>();
// ---- Navigator definition ----
function AppStack() {
return (
<Stack.Navigator
initialRouteName="Home"
screenOptions={{
// Header styling
headerStyle: { backgroundColor: '#0f172a' },
headerTintColor: '#f1f5f9', // back button + title color
headerTitleStyle: { fontWeight: '700' },
headerShadowVisible: false, // remove bottom border
// Transition
animation: 'slide_from_right', // iOS native slide | fade | slide_from_bottom
// Status bar
statusBarStyle: 'light',
statusBarColor: '#0f172a', // Android only
}}
>
<Stack.Screen
name="Home"
component={HomeScreen}
options={{ title: 'Discover', headerLargeTitle: true }} // iOS large title
/>
<Stack.Screen
name="ProductDetail"
component={ProductDetailScreen}
options={({ route }) => ({
title: route.params.product.name, // dynamic title from params
headerRight: () => <ShareButton />, // custom header button
})}
/>
{/* Modal presentation style */}
<Stack.Screen
name="Modal"
component={ModalScreen}
options={{ presentation: 'modal', animation: 'slide_from_bottom' }}
/>
</Stack.Navigator>
);
}
// ---- Navigating with type safety ----
function HomeScreen({ navigation }: HomeProps) {
return (
<View>
<Button title="Go to Product" onPress={() =>
navigation.navigate('ProductDetail', {
product: { id: '1', name: 'AirPods Pro' },
})
} />
<Button title="Replace (no back)" onPress={() =>
navigation.replace('Home') // no back button (used after login)
} />
<Button title="Reset stack" onPress={() =>
navigation.reset({ index: 0, routes: [{ name: 'Home' }] })
} />
</View>
);
}
// ---- Reading params ----
import { useRoute } from '@react-navigation/native';
type ProductDetailRoute = RouteProp<RootStackParamList, 'ProductDetail'>;
function ProductDetailScreen() {
const route = useRoute<ProductDetailRoute>();
const { product } = route.params; // typed!
return <Text>{product.name}</Text>;
}Tip
Tip
Practice Stack Navigator Screens Headers in small, isolated examples before integrating into larger projects. Breaking concepts into small experiments builds genuine understanding faster than reading alone.
Push, pop, and replace navigation mechanics with native OS transitions
Practice Task
Note
Practice Task - (1) Write a working example of Stack Navigator Screens Headers 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 Stack Navigator Screens Headers 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.
Finished this lesson?