Skip to content

Commit c79778c

Browse files
authored
feat: add snack to use-navigation-state (#617)
* feat: add snack to use-navigation-state * fix: change snack * fix: add requested changes
1 parent bb10637 commit c79778c

File tree

2 files changed

+92
-1
lines changed

2 files changed

+92
-1
lines changed

docs/use-navigation-state.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ const state = useNavigationState(state => state);
2222
2323
## How is `useNavigationState` different from `navigation.dangerouslyGetState()`?
2424

25-
The `navigation.dangerouslyGetState()` function also returns the current navigation state. The main difference is that the `useNavigation` hook will trigger a re-render when values change, while `navigation.dangerouslyGetState()` won't. For example, the following code will be incorrect:
25+
The `navigation.dangerouslyGetState()` function also returns the current navigation state. The main difference is that the `useNavigationState` hook will trigger a re-render when values change, while `navigation.dangerouslyGetState()` won't. For example, the following code will be incorrect:
2626

2727
```js
2828
function Profile() {
@@ -34,6 +34,8 @@ function Profile() {
3434

3535
In this example, even if you push a new screen, this text won't update. If you use the hook, it'll work as expected:
3636

37+
<samp id="use-navigation-state">
38+
3739
```js
3840
function Profile() {
3941
const routesLength = useNavigationState(state => state.routes.length);
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
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

Comments
 (0)