Skip to content

Commit 9c68751

Browse files
committed
Add snack links to navigating
1 parent ee10c0b commit 9c68751

File tree

1 file changed

+202
-21
lines changed

1 file changed

+202
-21
lines changed

versioned_docs/version-7.x/navigating.md

Lines changed: 202 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,14 @@ We'll do something similar to the latter, but rather than using a `window.locati
2828

2929
## Navigating to a new screen
3030

31-
<samp id="new-screen" />
32-
33-
```js
31+
```js name="Navigating to a new screen" snack version=7
32+
// codeblock-focus-start
3433
import * as React from 'react';
3534
import { Button, View, Text } from 'react-native';
36-
import { NavigationContainer, useNavigation } from '@react-navigation/native';
35+
import {
36+
createStaticNavigation,
37+
useNavigation,
38+
} from '@react-navigation/native';
3739
import { createNativeStackNavigator } from '@react-navigation/native-stack';
3840

3941
function HomeScreen() {
@@ -51,6 +53,29 @@ function HomeScreen() {
5153
}
5254

5355
// ... other code from the previous section
56+
// codeblock-focus-end
57+
58+
function DetailsScreen() {
59+
return (
60+
<View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
61+
<Text>Details Screen</Text>
62+
</View>
63+
);
64+
}
65+
66+
const RootStack = createNativeStackNavigator({
67+
initialRouteName: 'Home',
68+
screens: {
69+
Home: HomeScreen,
70+
Details: DetailsScreen,
71+
},
72+
});
73+
74+
const Navigation = createStaticNavigation(RootStack);
75+
76+
export default function App() {
77+
return <Navigation />;
78+
}
5479
```
5580

5681
Let's break this down:
@@ -66,11 +91,32 @@ If we call `navigation.navigate` with a route name that we haven't defined in a
6691

6792
So we now have a stack with two routes: 1) the `Home` route 2) the `Details` route. What would happen if we navigated to the `Details` route again, from the `Details` screen?
6893

69-
## Navigate to a route multiple times
94+
## Navigate to a screen multiple times
95+
96+
```js name="Navigate to a screen multiple times" snack version=7
97+
import * as React from 'react';
98+
import { Button, View, Text } from 'react-native';
99+
import {
100+
createStaticNavigation,
101+
useNavigation,
102+
} from '@react-navigation/native';
103+
import { createNativeStackNavigator } from '@react-navigation/native-stack';
70104

71-
<samp id="multiple-navigate" />
105+
function HomeScreen() {
106+
const navigation = useNavigation();
72107

73-
```js
108+
return (
109+
<View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
110+
<Text>Home Screen</Text>
111+
<Button
112+
title="Go to Details"
113+
onPress={() => navigation.navigate('Details')}
114+
/>
115+
</View>
116+
);
117+
}
118+
119+
// codeblock-focus-start
74120
function DetailsScreen() {
75121
const navigation = useNavigation();
76122

@@ -84,19 +130,79 @@ function DetailsScreen() {
84130
</View>
85131
);
86132
}
133+
// codeblock-focus-end
134+
135+
const RootStack = createNativeStackNavigator({
136+
initialRouteName: 'Home',
137+
screens: {
138+
Home: HomeScreen,
139+
Details: DetailsScreen,
140+
},
141+
});
142+
143+
const Navigation = createStaticNavigation(RootStack);
144+
145+
export default function App() {
146+
return <Navigation />;
147+
}
87148
```
88149

89-
If you run this code, you'll notice that when you tap "Go to Details... again" that it doesn't do anything! This is because we are already on the Details route. The `navigate` function roughly means "go to this screen", and if you are already on that screen then it makes sense that it would do nothing.
150+
If you run this code, you'll notice that when you tap "Go to Details... again", it doesn't do anything! This is because we are already on the Details route. The `navigate` function roughly means "go to this screen", and if you are already on that screen then it makes sense that it would do nothing.
90151

91152
Let's suppose that we actually _want_ to add another details screen. This is pretty common in cases where you pass in some unique data to each route (more on that later when we talk about `params`!). To do this, we can change `navigate` to `push`. This allows us to express the intent to add another route regardless of the existing navigation history.
92153

93-
<samp id="multiple-push" />
154+
```js name="Navigate to a screen multiple times" snack version=7
155+
import * as React from 'react';
156+
import { Button, View, Text } from 'react-native';
157+
import {
158+
createStaticNavigation,
159+
useNavigation,
160+
} from '@react-navigation/native';
161+
import { createNativeStackNavigator } from '@react-navigation/native-stack';
94162

95-
```js
96-
<Button
97-
title="Go to Details... again"
98-
onPress={() => navigation.push('Details')}
99-
/>
163+
function HomeScreen() {
164+
const navigation = useNavigation();
165+
166+
return (
167+
<View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
168+
<Text>Home Screen</Text>
169+
<Button
170+
title="Go to Details"
171+
onPress={() => navigation.navigate('Details')}
172+
/>
173+
</View>
174+
);
175+
}
176+
177+
function DetailsScreen() {
178+
const navigation = useNavigation();
179+
180+
return (
181+
<View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
182+
<Text>Details Screen</Text>
183+
// codeblock-focus-start
184+
<Button
185+
title="Go to Details... again"
186+
onPress={() => navigation.push('Details')}
187+
/>
188+
// codeblock-focus-end
189+
</View>
190+
);
191+
}
192+
193+
const RootStack = createNativeStackNavigator({
194+
initialRouteName: 'Home',
195+
screens: {
196+
Home: HomeScreen,
197+
Details: DetailsScreen,
198+
},
199+
});
200+
201+
const Navigation = createStaticNavigation(RootStack);
202+
203+
export default function App() {
204+
return <Navigation />;
205+
}
100206
```
101207

102208
<div style={{ display: 'flex', margin: '16px 0' }}>
@@ -111,11 +217,32 @@ Each time you call `push` we add a new route to the navigation stack. When you c
111217

112218
The header provided by the native stack navigator will automatically include a back button when it is possible to go back from the active screen (if there is only one screen in the navigation stack, there is nothing that you can go back to, and so there is no back button).
113219

114-
Sometimes you'll want to be able to programmatically trigger this behavior, and for that you can use `navigation.goBack();`.
220+
Sometimes you'll want to be able to programmatically trigger this behavior, and for that, you can use `navigation.goBack()`.
115221

116-
<samp id="go-back" />
222+
```js name="Going back" snack version=7
223+
import * as React from 'react';
224+
import { Button, View, Text } from 'react-native';
225+
import {
226+
createStaticNavigation,
227+
useNavigation,
228+
} from '@react-navigation/native';
229+
import { createNativeStackNavigator } from '@react-navigation/native-stack';
117230

118-
```js
231+
function HomeScreen() {
232+
const navigation = useNavigation();
233+
234+
return (
235+
<View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
236+
<Text>Home Screen</Text>
237+
<Button
238+
title="Go to Details"
239+
onPress={() => navigation.navigate('Details')}
240+
/>
241+
</View>
242+
);
243+
}
244+
245+
// codeblock-focus-start
119246
function DetailsScreen() {
120247
const navigation = useNavigation();
121248

@@ -126,11 +253,27 @@ function DetailsScreen() {
126253
title="Go to Details... again"
127254
onPress={() => navigation.push('Details')}
128255
/>
129-
<Button title="Go to Home" onPress={() => navigation.navigate('Home')} />
256+
// highlight-start
130257
<Button title="Go back" onPress={() => navigation.goBack()} />
258+
// highlight-end
131259
</View>
132260
);
133261
}
262+
// codeblock-focus-end
263+
264+
const RootStack = createNativeStackNavigator({
265+
initialRouteName: 'Home',
266+
screens: {
267+
Home: HomeScreen,
268+
Details: DetailsScreen,
269+
},
270+
});
271+
272+
const Navigation = createStaticNavigation(RootStack);
273+
274+
export default function App() {
275+
return <Navigation />;
276+
}
134277
```
135278

136279
:::note
@@ -141,9 +284,30 @@ On Android, React Navigation hooks in to the hardware back button and fires the
141284

142285
Another common requirement is to be able to go back _multiple_ screens -- for example, if you are several screens deep in a stack and want to dismiss all of them to go back to the first screen. In this case, we know that we want to go back to `Home` so we can use `popTo('Home')`. Another alternative would be `navigation.popToTop()`, which goes back to the first screen in the stack.
143286

144-
<samp id="pop-to-top" />
287+
```js name="Going back to specific screen" snack version=7
288+
import * as React from 'react';
289+
import { Button, View, Text } from 'react-native';
290+
import {
291+
createStaticNavigation,
292+
useNavigation,
293+
} from '@react-navigation/native';
294+
import { createNativeStackNavigator } from '@react-navigation/native-stack';
145295

146-
```js
296+
function HomeScreen() {
297+
const navigation = useNavigation();
298+
299+
return (
300+
<View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
301+
<Text>Home Screen</Text>
302+
<Button
303+
title="Go to Details"
304+
onPress={() => navigation.navigate('Details')}
305+
/>
306+
</View>
307+
);
308+
}
309+
310+
// codeblock-focus-start
147311
function DetailsScreen() {
148312
const navigation = useNavigation();
149313

@@ -154,15 +318,32 @@ function DetailsScreen() {
154318
title="Go to Details... again"
155319
onPress={() => navigation.push('Details')}
156320
/>
157-
<Button title="Go to Home" onPress={() => navigation.popTo('Home')} />
158321
<Button title="Go back" onPress={() => navigation.goBack()} />
322+
// highlight-start
323+
<Button title="Go to Home" onPress={() => navigation.popTo('Home')} />
159324
<Button
160325
title="Go back to first screen in stack"
161326
onPress={() => navigation.popToTop()}
162327
/>
328+
// highlight-end
163329
</View>
164330
);
165331
}
332+
// codeblock-focus-end
333+
334+
const RootStack = createNativeStackNavigator({
335+
initialRouteName: 'Home',
336+
screens: {
337+
Home: HomeScreen,
338+
Details: DetailsScreen,
339+
},
340+
});
341+
342+
const Navigation = createStaticNavigation(RootStack);
343+
344+
export default function App() {
345+
return <Navigation />;
346+
}
166347
```
167348

168349
## Summary

0 commit comments

Comments
 (0)