Created
July 8, 2022 11:54
-
-
Save josephajibodu/5b2b8cb2c911945347b362d1e0a2f818 to your computer and use it in GitHub Desktop.
React Navigation v6 with Typescript
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { createNativeStackNavigator, NativeStackScreenProps } from '@react-navigation/native-stack'; | |
import { Button, View } from 'react-native'; | |
export type RootStackParamList = { | |
Home: undefined; | |
Profile: undefined; | |
Settings: { | |
userId: number; | |
}; | |
}; | |
const Stack = createNativeStackNavigator<RootStackParamList>(); | |
<Stack.Navigator initialRouteName="Home"> | |
<Stack.Screen name="Home" component={() => <View />} /> | |
<Stack.Screen name="Profile" component={ProfileScreen} /> | |
<Stack.Screen name="Settings" component={() => <View />} /> | |
</Stack.Navigator> | |
type ProfileProps = NativeStackScreenProps<RootStackParamList, 'Profile'>; | |
// Every react-navigation screen receives route and navigation props | |
function ProfileScreen({ route, navigation }: ProfileProps) { | |
return (<View> | |
<Button | |
title='Go to Settings' | |
onPress={() => { | |
navigation.navigate('Settings', { | |
userId: 4 | |
}); | |
}} | |
/> | |
</View>) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment