Navigation (React Navigation)
Setup
- •Use
@react-navigation/nativewith@react-navigation/native-stackas default. - •Wrap app root in
NavigationContainer. - •Define navigators in dedicated
navigation/folder. - •One file per navigator (
RootNavigator.tsx,TabNavigator.tsx,AuthNavigator.tsx).
Typed Routes
- •Define
RootStackParamListtype for every navigator. - •Every route must have explicitly typed params (or
undefinedif none). - •Use typed
useNavigationanduseRoutehooks everywhere. - •Never pass untyped route params.
ts
type RootStackParamList = {
Home: undefined
Profile: { userId: string }
Settings: undefined
}
type ProfileScreenProps = NativeStackScreenProps<RootStackParamList, 'Profile'>
ts
const useAppNavigation = () => {
return useNavigation<NativeStackNavigationProp<RootStackParamList>>()
}
Navigator Structure
- •Use nested navigators for logical grouping (Auth stack, Main tabs, Modal stack).
- •Keep nesting shallow - max 3 levels.
- •Tab navigator inside root stack, not the other way around.
code
RootStack ├── AuthStack (Login, Register, ForgotPassword) ├── MainTabs │ ├── HomeStack │ ├── SearchStack │ └── ProfileStack └── Modal screens (presented over tabs)
Screen Options
- •Define
screenOptionson the navigator level for shared config. - •Override per-screen only when necessary.
- •Use
headerShown: falsewhen building custom headers. - •Respect platform conventions (iOS large titles, Android material appbar).
Deep Linking
- •Define
linkingconfig onNavigationContainer. - •Map every route to a URL path.
- •Handle nested navigators in linking config.
- •Test deep links on both platforms.
ts
const linking: LinkingOptions<RootStackParamList> = {
prefixes: ['myapp://', 'https://myapp.com'],
config: {
screens: {
Home: '',
Profile: 'profile/:userId',
Settings: 'settings',
},
},
}
Navigation Actions
- •Use
navigation.navigate()for standard transitions. - •Use
navigation.push()when you want to add to stack even if screen exists. - •Use
navigation.replace()after auth flow (no back to login). - •Use
CommonActions.reset()sparingly and only for auth state changes.
Rules
- •Never store navigation state manually - let React Navigation handle it.
- •Do not call navigation actions outside of React components or hooks.
- •Avoid passing complex objects as route params - pass IDs and fetch data on screen.
- •Keep route param types serializable (string, number, boolean).