React Native Cheatsheet
React Native Navigation
Use this React Native reference while you build software engineering projects, review code for technical interview prep, or polish examples for a software engineer resume.
TypeScript: Typed Params
// types/navigation.ts export type RootStackParamList = { Home: undefined; Details: { itemId: number; name: string }; Profile: { userId: string }; }; // Screen component import type { NativeStackScreenProps } from '@react-navigation/native-stack'; type Props = NativeStackScreenProps<RootStackParamList, 'Details'>; function DetailsScreen({ route, navigation }: Props) { const { itemId, name } = route.params; return <Text>{name}</Text>; } // useNavigation hook with types import { useNavigation } from '@react-navigation/native'; import type { NativeStackNavigationProp } from '@react-navigation/native-stack'; const navigation = useNavigation<NativeStackNavigationProp<RootStackParamList>>();
Route Params
import { useRoute } from '@react-navigation/native'; function DetailsScreen() { const route = useRoute(); const { itemId, name } = route.params as { itemId: number; name: string }; return <Text>{name}</Text>; } // From screen props (preferred with TypeScript) function DetailsScreen({ route, navigation }: Props) { const { itemId } = route.params; }
Deep Linking
const linking = { prefixes: ['myapp://', 'https://myapp.com'], config: { screens: { Home: 'home', Details: 'details/:itemId', Profile: { path: 'user/:userId', parse: { userId: (id: string) => parseInt(id) }, }, }, }, }; <NavigationContainer linking={linking} fallback={<ActivityIndicator />}> ... </NavigationContainer>
Expo Router (File-based routing)
npx create-expo-app MyApp --template tabs
app/ ├── _layout.tsx # Root layout (NavigationContainer equivalent) ├── index.tsx # "/" route ├── (tabs)/ │ ├── _layout.tsx # Tab layout │ ├── home.tsx # "/home" │ └── profile.tsx # "/profile" └── details/ └── [id].tsx # "/details/123"
// app/(tabs)/_layout.tsx import { Tabs } from 'expo-router'; export default function TabLayout() { return ( <Tabs> <Tabs.Screen name="home" options={{ title: 'Home' }} /> <Tabs.Screen name="profile" options={{ title: 'Profile' }} /> </Tabs> ); } // Navigation in components import { router, useLocalSearchParams } from 'expo-router'; router.push('/details/42'); router.replace('/login'); router.back(); // In details/[id].tsx const { id } = useLocalSearchParams();