Skip to content

Commit f111a4c

Browse files
committed
Document popTo action
1 parent 8cd7275 commit f111a4c

8 files changed

Lines changed: 79 additions & 41 deletions

File tree

static/examples/7.x/pop-to-top.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ function DetailsScreen({ navigation }) {
2323
title="Go to Details... again"
2424
onPress={() => navigation.push('Details')}
2525
/>
26-
<Button title="Go to Home" onPress={() => navigation.navigate('Home')} />
26+
<Button title="Go to Home" onPress={() => navigation.popTo('Home')} />
2727
<Button title="Go back" onPress={() => navigation.goBack()} />
2828
<Button
2929
title="Go back to first screen in stack"

versioned_docs/version-7.x/native-stack-navigator.md

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -650,9 +650,11 @@ React.useEffect(() => {
650650

651651
### Helpers
652652

653+
The nativestack navigator adds the following methods to the navigation prop:
654+
653655
#### `replace`
654656

655-
Replaces the current screen with a new screen in the stack. The method accepts following arguments:
657+
Replaces the current screen with a new screen in the stack. The method accepts the following arguments:
656658

657659
- `name` - _string_ - Name of the route to push onto the stack.
658660
- `params` - _object_ - Screen params to pass to the destination route.
@@ -663,7 +665,7 @@ navigation.replace('Profile', { owner: 'Michaś' });
663665

664666
#### `push`
665667

666-
Pushes a new screen to top of the stack and navigate to it. The method accepts following arguments:
668+
Pushes a new screen to the top of the stack and navigate to it. The method accepts the following arguments:
667669

668670
- `name` - _string_ - Name of the route to push onto the stack.
669671
- `params` - _object_ - Screen params to pass to the destination route.
@@ -680,6 +682,19 @@ Pops the current screen from the stack and navigates back to the previous screen
680682
navigation.pop();
681683
```
682684

685+
#### `popTo`
686+
687+
Navigates back to a previous screen in the stack by popping screens after it. The method accepts the following arguments:
688+
689+
- `name` - _string_ - Name of the route to navigate to.
690+
- `params` - _object_ - Screen params to pass to the destination route.
691+
692+
If a matching screen is not found in the stack, this will pop the current screen and add a new screen with the specified name and params.
693+
694+
```js
695+
navigation.popTo('Profile', { owner: 'Michaś' });
696+
```
697+
683698
#### `popToTop`
684699

685700
Pops all of the screens in the stack except the first one and navigates to it.

versioned_docs/version-7.x/navigating.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ On Android, React Navigation hooks in to the hardware back button and fires the
133133

134134
:::
135135

136-
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 `navigate('Home')` (not `push`! try that out and see the difference). Another alternative would be `navigation.popToTop()`, which goes back to the first screen in the stack.
136+
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.
137137

138138
<samp id="pop-to-top" />
139139

@@ -146,7 +146,7 @@ function DetailsScreen({ navigation }) {
146146
title="Go to Details... again"
147147
onPress={() => navigation.push('Details')}
148148
/>
149-
<Button title="Go to Home" onPress={() => navigation.navigate('Home')} />
149+
<Button title="Go to Home" onPress={() => navigation.popTo('Home')} />
150150
<Button title="Go back" onPress={() => navigation.goBack()} />
151151
<Button
152152
title="Go back to first screen in stack"
@@ -162,5 +162,5 @@ function DetailsScreen({ navigation }) {
162162
- `navigation.navigate('RouteName')` pushes a new route to the native stack navigator if it's not already in the stack, otherwise it jumps to that screen.
163163
- We can call `navigation.push('RouteName')` as many times as we like and it will continue pushing routes.
164164
- The header bar will automatically show a back button, but you can programmatically go back by calling `navigation.goBack()`. On Android, the hardware back button just works as expected.
165-
- You can go back to an existing screen in the stack with `navigation.navigate('RouteName')`, and you can go back to the first screen in the stack with `navigation.popToTop()`.
165+
- You can go back to an existing screen in the stack with `navigation.popTo('RouteName')`, and you can go back to the first screen in the stack with `navigation.popToTop()`.
166166
- The `navigation` prop is available to all screen components (components defined as screens in route configuration and rendered by React Navigation as a route).

versioned_docs/version-7.x/navigation-actions.md

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,8 @@ The library exports several action creators under the `CommonActions` namespace.
2323

2424
The `navigate` action allows to navigate to a specific route. It takes the following arguments:
2525

26-
- `name` - _string_ - A destination name of the route that has been registered somewhere..
27-
- `key` - _string_ - The identifier for the route to navigate to. Navigate back to this route if it already exists..
28-
- `params` - _object_ - Params to merge into the destination route..
29-
30-
The options object passed should at least contain a `key` or `name` property, and optionally `params`. If both `key` and `name` are passed, stack navigator will create a new route with the specified key if no matches were found.
26+
- `name` - _string_ - A destination name of the screen in the current or a parent navigator.
27+
- `params` - _object_ - Params to use for the destination route.
3128

3229
<samp id="common-actions" />
3330

@@ -44,9 +41,18 @@ navigation.dispatch(
4441
);
4542
```
4643

47-
In a [stack navigator](stack-navigator.md), calling `navigate` with a screen name will result in different behavior based on if the screen is already present or not. If the screen is already present in the stack's history, it'll go back to that screen and remove any screens after that. If the screen is not present, it'll push a new screen.
44+
In a stack navigator ([stack](stack-navigator.md) or [native stack](native-stack-navigator.md)), calling `navigate` with a screen name will have the following behavior:
45+
46+
- If you're already on a screen with the same name, it will update its params and not push a new screen.
47+
- If you're on a different screen, it will push the new screen onto the stack.
48+
- If the [`getId`](screen.md#getid) prop is specified, and another screen in the stack has the same ID, it will navigate to that screen and update its params instead.
49+
50+
The `navigate` action can also accepts an object as the argument with the following properties:
4851

49-
By default, the screen is identified by its name. But you can also customize it to take the params into account by using the [`getId`](screen.md#getid) prop.
52+
- `name` - _string_ - A destination name of the screen in the current or a parent navigator
53+
- `params` - _object_ - Params to use for the destination route.
54+
- `merge` - _boolean_ - Whether we should merge the params of the current route with the provided `params`. Defaults to `false`.
55+
- `path` - _string_ - The path (from deep link or universal link) to associate with the screen.
5056

5157
### reset
5258

versioned_docs/version-7.x/navigation-prop.md

Lines changed: 5 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ If the navigator is a stack navigator, several alternatives to `navigate` and `g
3030
- `replace` - replace the current screen with a new one
3131
- `push` - push a new screen onto the stack
3232
- `pop` - go back in the stack
33+
- `popTo` - go back to a specific screen in the stack
3334
- `popToTop` - go to the top of the stack
3435

3536
See [Stack navigator helpers](stack-navigator.md#helpers) and [Native Stack navigator helpers](native-stack-navigator.md#helpers) for more details on these methods.
@@ -82,9 +83,11 @@ function HomeScreen({ navigation: { navigate } }) {
8283
}
8384
```
8485

85-
In a [native stack navigator](native-stack-navigator.md), calling `navigate` with a screen name will result in different behavior based on if the screen is already present or not. If the screen is already present in the stack's history, it'll go back to that screen and remove any screens after that. If the screen is not present, it'll push a new screen.
86+
In a stack navigator ([stack](stack-navigator.md) or [native stack](native-stack-navigator.md)), calling `navigate` with a screen name will have the following behavior:
8687

87-
For example, if you have a stack with the history `Home > Profile > Settings` and you call `navigate(Profile)`, the resulting screens will be `Home > Profile` as it goes back to `Profile` and removes the `Settings` screen.
88+
- If you're already on a screen with the same name, it will update its params and not push a new screen.
89+
- If you're on a different screen, it will push the new screen onto the stack.
90+
- If the [`getId`](screen.md#getid) prop is specified, and another screen in the stack has the same ID, it will navigate to that screen and update its params instead.
8891

8992
By default, the screen is identified by its name. But you can also customize it to take the params into account by using the [`getId`](screen.md#getid) prop.
9093

@@ -114,26 +117,6 @@ function ProfileScreen({ navigation: { goBack } }) {
114117
}
115118
```
116119

117-
#### Going back from a specific screen
118-
119-
Consider the following navigation stack history:
120-
121-
```javascript
122-
navigation.navigate({ name: SCREEN, key: SCREEN_KEY_A });
123-
navigation.navigate({ name: SCREEN, key: SCREEN_KEY_B });
124-
navigation.navigate({ name: SCREEN, key: SCREEN_KEY_C });
125-
navigation.navigate({ name: SCREEN, key: SCREEN_KEY_D });
126-
```
127-
128-
Now you are on _screen D_ and want to go back to _screen A_ (popping D, C, and B).
129-
Then you can use `navigate`:
130-
131-
```js
132-
navigation.navigate({ key: SCREEN_KEY_A }); // will go to screen A FROM screen D
133-
```
134-
135-
Alternatively, as _screen A_ is the top of the stack, you can use `navigation.popToTop()`.
136-
137120
### `reset`
138121

139122
The `reset` method lets us replace the navigator state with a new state:

versioned_docs/version-7.x/stack-actions.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,27 @@ const popAction = StackActions.pop(1);
7676
navigation.dispatch(popAction);
7777
```
7878

79+
### popTo
80+
81+
The `popTo` action takes you back to a previous screen in the stack by the name. It also allows you to pass params to the route.
82+
83+
If a matching screen is not found in the stack, this will pop the current screen and add a new screen with the specified name and params. This behavior is useful when the screen was opened from a deep link etc. and a previous screen with the name may or may not already be in the stack.
84+
85+
The method accepts the following arguments:
86+
87+
- `name` - _string_ - Name of the route to navigate to.
88+
- `params` - _object_ - Screen params to pass to the destination route.
89+
90+
If a matching screen is not found in the stack, this will pop the current screen and add a new screen with the specified name and params. This behavior is useful when the screen was opened from a deep link and a previous screen with the name may or may not already be in the stack.
91+
92+
```js
93+
import { StackActions } from '@react-navigation/native';
94+
95+
const popToAction = StackActions.popTo('Profile', { user: 'jane' });
96+
97+
navigation.dispatch(popToAction);
98+
```
99+
79100
### popToTop
80101

81102
The `popToTop` action takes you back to the first screen in the stack, dismissing all the others. It's functionally identical to `StackActions.pop({n: currentIndex})`.

versioned_docs/version-7.x/stack-navigator.md

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,9 @@ Then, you need to install and configure the libraries that are required by the s
6868

6969
4. If you're on a Mac and developing for iOS, you also need to install the pods (via [Cocoapods](https://cocoapods.org/)) to complete the linking.
7070

71-
```bash
72-
npx pod-install ios
73-
```
71+
```bash
72+
npx pod-install ios
73+
```
7474

7575
## API Definition
7676

@@ -457,7 +457,7 @@ The stack navigator adds the following methods to the navigation prop:
457457

458458
#### `replace`
459459

460-
Replaces the current screen with a new screen in the stack. The method accepts following arguments:
460+
Replaces the current screen with a new screen in the stack. The method accepts the following arguments:
461461

462462
- `name` - _string_ - Name of the route to push onto the stack.
463463
- `params` - _object_ - Screen params to pass to the destination route.
@@ -468,7 +468,7 @@ navigation.replace('Profile', { owner: 'Michaś' });
468468

469469
#### `push`
470470

471-
Pushes a new screen to top of the stack and navigate to it. The method accepts following arguments:
471+
Pushes a new screen to the top of the stack and navigate to it. The method accepts the following arguments:
472472

473473
- `name` - _string_ - Name of the route to push onto the stack.
474474
- `params` - _object_ - Screen params to pass to the destination route.
@@ -485,6 +485,19 @@ Pops the current screen from the stack and navigates back to the previous screen
485485
navigation.pop();
486486
```
487487

488+
#### `popTo`
489+
490+
Navigates back to a previous screen in the stack by popping screens after it. The method accepts the following arguments:
491+
492+
- `name` - _string_ - Name of the route to navigate to.
493+
- `params` - _object_ - Screen params to pass to the destination route.
494+
495+
If a matching screen is not found in the stack, this will pop the current screen and add a new screen with the specified name and params.
496+
497+
```js
498+
navigation.popTo('Profile', { owner: 'Michaś' });
499+
```
500+
488501
#### `popToTop`
489502

490503
Pops all of the screens in the stack except the first one and navigates to it.

versioned_docs/version-7.x/upgrading-from-6.x.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ The `navigationInChildEnabled` prop will be removed in the next major.
3737

3838
Previously, `navigate` method navigated back if the screen already exists in the stack. We have seen many people get confused by this behavior.
3939

40-
To avoid this confusion, we have removed the going back behavior from `navigate` and added a new method `popTo` to explicitly go back to a specific screen in the stack:
40+
To avoid this confusion, we have removed the going back behavior from `navigate` and added a [new method `popTo`](stack-actions.md#popto) to explicitly go back to a specific screen in the stack:
4141

4242
```diff
4343
- navigation.navigate('PreviousScreen', { foo: 42 });

0 commit comments

Comments
 (0)