|
| 1 | +import * as React from 'react'; |
| 2 | +import { Button, View, Text } from 'react-native'; |
| 3 | +import { |
| 4 | + NavigationNativeContainer, |
| 5 | + useRoute, |
| 6 | + useNavigationState, |
| 7 | +} from '@react-navigation/native'; |
| 8 | +import { createStackNavigator } from '@react-navigation/stack'; |
| 9 | + |
| 10 | +function useIsFirstRouteInParent() { |
| 11 | + const route = useRoute(); |
| 12 | + const isFirstRouteInParent = useNavigationState( |
| 13 | + state => state.routes[0].key === route.key |
| 14 | + ); |
| 15 | + |
| 16 | + return isFirstRouteInParent; |
| 17 | +} |
| 18 | + |
| 19 | +function usePreviousRouteName() { |
| 20 | + return useNavigationState(state => |
| 21 | + state.routes[state.index - 1]?.name |
| 22 | + ? state.routes[state.index - 1].name |
| 23 | + : 'None' |
| 24 | + ); |
| 25 | +} |
| 26 | + |
| 27 | +function HomeScreen({ navigation }) { |
| 28 | + const isFirstRoute = useIsFirstRouteInParent(); |
| 29 | + const previousRouteName = usePreviousRouteName(); |
| 30 | + return ( |
| 31 | + <View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}> |
| 32 | + <Text>It is {isFirstRoute ? '' : 'not '}first route in navigator</Text> |
| 33 | + <Text>Previous route name: {previousRouteName}</Text> |
| 34 | + |
| 35 | + <Button |
| 36 | + title="Go to Profile" |
| 37 | + onPress={() => navigation.navigate('Profile')} |
| 38 | + /> |
| 39 | + </View> |
| 40 | + ); |
| 41 | +} |
| 42 | + |
| 43 | +function ProfileScreen({ navigation }) { |
| 44 | + const isFirstRoute = useIsFirstRouteInParent(); |
| 45 | + const previousRouteName = usePreviousRouteName(); |
| 46 | + return ( |
| 47 | + <View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}> |
| 48 | + <Text>It is {isFirstRoute ? '' : 'not '}first route in navigator</Text> |
| 49 | + <Text>Previous route name: {previousRouteName}</Text> |
| 50 | + <Button |
| 51 | + title="Go to Settings" |
| 52 | + onPress={() => navigation.navigate('Settings')} |
| 53 | + /> |
| 54 | + <Button title="Go back" onPress={() => navigation.goBack()} /> |
| 55 | + </View> |
| 56 | + ); |
| 57 | +} |
| 58 | + |
| 59 | +function SettingsScreen({ navigation }) { |
| 60 | + const isFirstRoute = useIsFirstRouteInParent(); |
| 61 | + const previousRouteName = usePreviousRouteName(); |
| 62 | + return ( |
| 63 | + <View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}> |
| 64 | + <Text>It is {isFirstRoute ? '' : 'not '}first route in navigator</Text> |
| 65 | + <Text>Previous route name: {previousRouteName}</Text> |
| 66 | + <Button title="Go back" onPress={() => navigation.goBack()} /> |
| 67 | + </View> |
| 68 | + ); |
| 69 | +} |
| 70 | + |
| 71 | +const Stack = createStackNavigator(); |
| 72 | + |
| 73 | +function MyStack() { |
| 74 | + return ( |
| 75 | + <Stack.Navigator> |
| 76 | + <Stack.Screen name="Home" component={HomeScreen} /> |
| 77 | + <Stack.Screen name="Profile" component={ProfileScreen} /> |
| 78 | + <Stack.Screen name="Settings" component={SettingsScreen} /> |
| 79 | + </Stack.Navigator> |
| 80 | + ); |
| 81 | +} |
| 82 | + |
| 83 | +export default function App() { |
| 84 | + return ( |
| 85 | + <NavigationNativeContainer> |
| 86 | + <MyStack /> |
| 87 | + </NavigationNativeContainer> |
| 88 | + ); |
| 89 | +} |
0 commit comments