Skip to content

Commit 8e23dc1

Browse files
committed
Mention link and button in fundamentals
1 parent 051e927 commit 8e23dc1

File tree

1 file changed

+83
-17
lines changed

1 file changed

+83
-17
lines changed

versioned_docs/version-7.x/navigating.md

Lines changed: 83 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -15,28 +15,89 @@ If this was a web browser, we'd be able to write something like this:
1515
<a href="details.html">Go to Details</a>
1616
```
1717

18-
Another way to write this would be:
18+
It's also possible to programmatically change the URL using JavaScript:
1919

2020
```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';
2822
```
2923

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:
3125

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
31+
import * as React from 'react';
32+
import { View, Text } from 'react-native';
33+
import { createStaticNavigation } from '@react-navigation/native';
34+
import { createNativeStackNavigator } from '@react-navigation/native-stack';
35+
// highlight-start
36+
import { Link } from '@react-navigation/native';
37+
import { Button } from '@react-navigation/elements';
38+
// highlight-end
39+
40+
function HomeScreen() {
41+
return (
42+
<View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
43+
<Text>Home Screen</Text>
44+
// highlight-start
45+
<Link screen="Details">Go to Details</Link>
46+
<Button screen="Details">Go to Details</Button>
47+
// highlight-end
48+
</View>
49+
);
50+
}
51+
52+
function DetailsScreen() {
53+
return (
54+
<View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
55+
<Text>Details Screen</Text>
56+
</View>
57+
);
58+
}
59+
60+
const RootStack = createNativeStackNavigator({
61+
initialRouteName: 'Home',
62+
screens: {
63+
Home: HomeScreen,
64+
Details: DetailsScreen,
65+
},
66+
});
67+
68+
const Navigation = createStaticNavigation(RootStack);
69+
70+
export default function App() {
71+
return <Navigation />;
72+
}
73+
```
74+
75+
Let's break this down:
76+
77+
- 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:
3393

3494
```js name="Navigating to a new screen" snack static2dynamic
3595
// codeblock-focus-start
3696
import * as React from 'react';
3797
import { View, Text } from 'react-native';
3898
import {
3999
createStaticNavigation,
100+
// highlight-next-line
40101
useNavigation,
41102
} from '@react-navigation/native';
42103
import { createNativeStackNavigator } from '@react-navigation/native-stack';
@@ -90,19 +151,19 @@ export default function App() {
90151

91152
Let's break this down:
92153

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 &mdash; 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 &mdash; naming is hard!) with the name of the route that we'd like to move the user to.
95156

96157
:::note
97158

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 &mdash; 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.
99160

100161
:::
101162

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-
104163
## Navigate to a screen multiple times
105164

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+
106167
```js name="Navigate to a screen multiple times" snack static2dynamic
107168
import * as React from 'react';
108169
import { View, Text } from 'react-native';
@@ -289,11 +350,14 @@ export default function App() {
289350

290351
:::note
291352

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.
293354

294355
:::
295356

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
297361

298362
```js name="Going back to specific screen" snack static2dynamic
299363
import * as React from 'react';
@@ -361,6 +425,8 @@ export default function App() {
361425

362426
## Summary
363427

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.
364430
- [`navigation.navigate('RouteName')`](navigation-object.md#navigate) pushes a new route to the native stack navigator if you're not already on that route.
365431
- We can call [`navigation.push('RouteName')`](stack-actions.md#push) as many times as we like and it will continue pushing routes.
366432
- 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

Comments
 (0)