Skip to content

Commit 873f292

Browse files
authored
docs: mention index is always last route in a stack (react-navigation#1106)
Mention this in case a developer tries to specify a value for index apart that is not equal to the last route in a stack and sees that it has no effect, eg. issue #10056. Signed-off-by: Ray Chuan Tay <[email protected]> Signed-off-by: Ray Chuan Tay <[email protected]>
1 parent 9f6fa49 commit 873f292

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed

versioned_docs/version-5.x/navigation-state.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,20 @@ It's important to note that even if there's a nested navigator, the `state` prop
7373

7474
Earlier there was a mention of `stale` property in the navigation state. A stale navigation state means that the state object needs to be rehydrated or fixed or fixed up, such as adding missing keys, removing invalid screens etc. before being used. As a user, you don't need to worry about it, React Navigation will fix up any issues in a state object automatically unless `stale` is set to `false`. If you're writing a [custom router](custom-routers.md), the `getRehydratedState` method let's you write custom rehydration logic to fix up state objects.
7575

76+
This also applies to the `index` property: `index` should be the last route in a stack, and if a different value was specified, React Navigation fixes it. For example, if you wanted to reset your app's navigation state to have it display the `Profile` route, and have the `Home` route displayed upon going back, and did the below,
77+
78+
```js
79+
navigation.reset({
80+
index: 0,
81+
routes: [
82+
{ name: 'Home' },
83+
{ name: 'Profile' }
84+
],
85+
});
86+
```
87+
88+
React Navigation would correct `index` to 1, and display the route and perform navigation as intended.
89+
7690
This feature comes handy when doing operations such as [reset](navigation-actions.md#reset), [providing a initial state](navigation-container.md#initial-state) etc., as you can safely omit many properties from the navigation state object and relying on React Navigation to add those properties for you, making your code simpler. For example, you can only provide a `routes` array without any keys and React Navigation will automatically add everything that's needed to make it work:
7791

7892
```js

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,20 @@ It's important to note that even if there's a nested navigator, the `state` prop
7373

7474
Earlier there was a mention of `stale` property in the navigation state. A stale navigation state means that the state object needs to be rehydrated or fixed or fixed up, such as adding missing keys, removing invalid screens etc. before being used. As a user, you don't need to worry about it, React Navigation will fix up any issues in a state object automatically unless `stale` is set to `false`. If you're writing a [custom router](custom-routers.md), the `getRehydratedState` method let's you write custom rehydration logic to fix up state objects.
7575

76+
This also applies to the `index` property: `index` should be the last route in a stack, and if a different value was specified, React Navigation fixes it. For example, if you wanted to reset your app's navigation state to have it display the `Profile` route, and have the `Home` route displayed upon going back, and did the below,
77+
78+
```js
79+
navigation.reset({
80+
index: 0,
81+
routes: [
82+
{ name: 'Home' },
83+
{ name: 'Profile' }
84+
],
85+
});
86+
```
87+
88+
React Navigation would correct `index` to 1, and display the route and perform navigation as intended.
89+
7690
This feature comes handy when doing operations such as [reset](navigation-actions.md#reset), [providing a initial state](navigation-container.md#initial-state) etc., as you can safely omit many properties from the navigation state object and relying on React Navigation to add those properties for you, making your code simpler. For example, you can only provide a `routes` array without any keys and React Navigation will automatically add everything that's needed to make it work:
7791

7892
```js

0 commit comments

Comments
 (0)