You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: versioned_docs/version-7.x/navigating.md
+83-17Lines changed: 83 additions & 17 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -15,28 +15,89 @@ If this was a web browser, we'd be able to write something like this:
15
15
<a href="details.html">Go to Details</a>
16
16
```
17
17
18
-
Another way to write this would be:
18
+
It's also possible to programmatically change the URL using JavaScript:
19
19
20
20
```js
21
-
<a
22
-
onClick={() => {
23
-
window.location.href='details.html';
24
-
}}
25
-
>
26
-
Go to Details
27
-
</a>
21
+
window.location.href='details.html';
28
22
```
29
23
30
-
We'll do something similar to the latter, but rather than using a `window.location` global, we'll use the `navigation` object that's accessible in our screen components.
24
+
So how do we do this in React Navigation? There are two main ways to navigate between screens in React Navigation:
31
25
32
-
## Navigating to a new screen
26
+
## Using `Link` or `Button` components
27
+
28
+
The simplest way to navigate is by using the [`Link`](link.md) component from `@react-navigation/native` or the [`Button`](elements.md#button) component from `@react-navigation/elements`:
29
+
30
+
```js name="Navigation with Link and Button" snack static2dynamic
- The `Link` and `Button` components accept a `screen` prop that specifies the name of the screen to navigate to when pressed.
78
+
- When the user taps on the `Link` or `Button`, React Navigation automatically navigates to the specified screen.
79
+
80
+
[When using on the web](web-support.md), they also render as anchor tags (`<a>`) with `href` attribute, which is essential to preserve native browser behaviors like "Right click → Open link in new tab".
81
+
82
+
:::note
83
+
84
+
The built-in `Link` and `Button` components come with their own styling. But it's likely that you'll want to create your own custom link or button component to match your app's design. See [`useLinkProps`](use-link-props.md) hook on how to create custom link components.
85
+
86
+
:::
87
+
88
+
## Using the `navigation` object
89
+
90
+
Another way to navigate is by using the `navigation` object. This method gives you more control over when and how navigation happens.
91
+
92
+
The `navigation` object is available in your screen components through the [`useNavigation`](use-navigation.md) hook:
33
93
34
94
```js name="Navigating to a new screen" snack static2dynamic
@@ -90,19 +151,19 @@ export default function App() {
90
151
91
152
Let's break this down:
92
153
93
-
-`navigation` - the`navigation` object is returned from the [`useNavigation`](use-navigation.md) hook (more about this later in ["The navigation object in depth"](navigation-object.md)).
94
-
-`navigate('Details')` - we call the `navigate` function (on the `navigation` object — naming is hard!) with the name of the route that we'd like to move the user to.
154
+
-The`navigation` object is returned from the [`useNavigation`](use-navigation.md) hook (more about this later in ["The navigation object in depth"](navigation-object.md)).
155
+
-We call the `navigate` function (on the `navigation` object — naming is hard!) with the name of the route that we'd like to move the user to.
95
156
96
157
:::note
97
158
98
-
If we call `navigation.navigate` with a route name that we haven't defined in a navigator, it'll print an error in development builds and nothing will happen in production builds. Said another way, we can only navigate to routes that have been defined on our navigator— we cannot navigate to an arbitrary component.
159
+
If you call `navigation.navigate` with a route name that you haven't defined in your navigator, you'll see an error in development builds and nothing will happen in production builds. You can only navigate to routes that have been defined in your navigator.
99
160
100
161
:::
101
162
102
-
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?
103
-
104
163
## Navigate to a screen multiple times
105
164
165
+
So we now have a stack with two routes: the `Home` route and the `Details` route. What would happen if we navigated to the `Details` route again from the `Details` screen?
166
+
106
167
```js name="Navigate to a screen multiple times" snack static2dynamic
107
168
import*asReactfrom'react';
108
169
import { View, Text } from'react-native';
@@ -289,11 +350,14 @@ export default function App() {
289
350
290
351
:::note
291
352
292
-
On Android, React Navigation hooks in to the hardware back button and fires the`goBack()`function for you when the user presses it, so it behaves as the user would expect.
353
+
On Android, React Navigation hooks into the hardware back button and automatically calls`goBack()` when the user presses it, so it behaves as expected.
293
354
294
355
:::
295
356
296
-
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.
357
+
Sometimes you need to go back _multiple_ screens at once. For example, if you're several screens deep in a stack and want to go back to the first screen. You have two options:
358
+
359
+
-`navigation.popTo('Home')` - Go back to a specific screen (in this case, Home)
360
+
-`navigation.popToTop()` - Go back to the first screen in the stack
297
361
298
362
```js name="Going back to specific screen" snack static2dynamic
299
363
import*asReactfrom'react';
@@ -361,6 +425,8 @@ export default function App() {
361
425
362
426
## Summary
363
427
428
+
-[`Link`](link.md) and [`Button`](elements.md#button) components can be used to navigate between screens declaratively.
429
+
- We can use [`useLinkProps`](use-link-props.md) to create our own link components.
364
430
-[`navigation.navigate('RouteName')`](navigation-object.md#navigate) pushes a new route to the native stack navigator if you're not already on that route.
365
431
- We can call [`navigation.push('RouteName')`](stack-actions.md#push) as many times as we like and it will continue pushing routes.
366
432
- The header bar will automatically show a back button, but you can programmatically go back by calling [`navigation.goBack()`](navigation-object.md#goback). On Android, the hardware back button just works as expected.
0 commit comments