Skip to content

Commit c376471

Browse files
committed
Update recommendations for reset
1 parent 9c58986 commit c376471

File tree

2 files changed

+24
-4
lines changed

2 files changed

+24
-4
lines changed

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

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,9 @@ If you want to preserve the existing screens but only want to modify the state,
8181
import { CommonActions } from '@react-navigation/native';
8282

8383
navigation.dispatch(state => {
84-
// Remove the home route from the stack
85-
const routes = state.routes.filter(r => r.name !== 'Home');
84+
// Remove all the screens after `Profile`
85+
const index = state.routes.findIndex(r => r.name === 'Profile');
86+
const routes = state.routes.slice(0, index + 1);
8687

8788
return CommonActions.reset({
8889
...state,
@@ -98,6 +99,15 @@ Consider the navigator's state object to be internal and subject to change in a
9899

99100
:::
100101

102+
#### Rewriting the history with `reset`
103+
104+
Since the `reset` action can update the navigation state with a new state object, it can be used to rewrite the navigation history. However, rewriting the history to alter the back stack is not recommended in most cases:
105+
106+
- It can lead to a confusing user experience, as users expect to be able to go back to the screen they were on before.
107+
- When supporting the Web platform, the browser's history will still reflect the old navigation state, so users will see the old screen if they use the browser's back button - resulting in 2 different experiences depending on which back button the user presses.
108+
109+
So if you have such a use case, consider a different approach - e.g. updating the history once the user navigates back to the screen that has changed.
110+
101111
### goBack
102112

103113
The `goBack` action creator allows to go back to the previous route in history. It doesn't take any arguments.

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

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,9 @@ If you want to preserve the existing screens but only want to modify the state,
8787
import { CommonActions } from '@react-navigation/native';
8888

8989
navigation.dispatch(state => {
90-
// Remove the home route from the stack
91-
const routes = state.routes.filter(r => r.name !== 'Home');
90+
// Remove all the screens after `Profile`
91+
const index = state.routes.findIndex(r => r.name === 'Profile');
92+
const routes = state.routes.slice(0, index + 1);
9293

9394
return CommonActions.reset({
9495
...state,
@@ -104,6 +105,15 @@ Consider the navigator's state object to be internal and subject to change in a
104105

105106
:::
106107

108+
#### Rewriting the history with `reset`
109+
110+
Since the `reset` action can update the navigation state with a new state object, it can be used to rewrite the navigation history. However, rewriting the history to alter the back stack is not recommended in most cases:
111+
112+
- It can lead to a confusing user experience, as users expect to be able to go back to the screen they were on before.
113+
- When supporting the Web platform, the browser's history will still reflect the old navigation state, so users will see the old screen if they use the browser's back button - resulting in 2 different experiences depending on which back button the user presses.
114+
115+
So if you have such a use case, consider a different approach - e.g. updating the history once the user navigates back to the screen that has changed.
116+
107117
### goBack
108118

109119
The `goBack` action creator allows to go back to the previous route in history. It doesn't take any arguments.

0 commit comments

Comments
 (0)