Skip to content

Commit 09bd82d

Browse files
committed
Document changes to getId behavior
1 parent 5b1386b commit 09bd82d

7 files changed

Lines changed: 36 additions & 35 deletions

File tree

versioned_docs/version-8.x/bottom-tab-navigator.md

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -557,6 +557,15 @@ The icon object can be one of the following types:
557557

558558
It's necessary to provide icons for multiple screen densities (1x, 2x, 3x), e.g.: `icon.png`, `icon@2x.png`, `icon@3x.png` etc. as icons are not scaled automatically on iOS for the `native` implementation.
559559

560+
It also supports [drawable resource](https://developer.android.com/guide/topics/resources/drawable-resource) on Android, [xcasset](https://developer.apple.com/documentation/xcode/adding-images-to-your-xcode-project) on iOS:
561+
562+
```js
563+
tabBarIcon: {
564+
type: 'image',
565+
source: { uri: 'icon_name' },
566+
}
567+
```
568+
560569
A `tinted` property can be used to control whether the icon should be tinted with the active/inactive color:
561570

562571
```js
@@ -597,15 +606,6 @@ The icon object can be one of the following types:
597606

598607
See [Icons](icons.md#material-symbols) for more details.
599608

600-
- Resource name - [Drawable resource](https://developer.android.com/guide/topics/resources/drawable-resource) on Android, [xcasset](https://developer.apple.com/documentation/xcode/adding-images-to-your-xcode-project) on iOS.
601-
602-
```js
603-
tabBarIcon: {
604-
type: 'resource',
605-
name: 'sunny',
606-
}
607-
```
608-
609609
To render different icons for active and inactive states, you can use a function:
610610

611611
```js

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1543,6 +1543,8 @@ If a matching screen is not found in the stack, this will pop the current screen
15431543
navigation.popTo('Profile', { owner: 'Michaś' });
15441544
```
15451545
1546+
If [`getId`](screen.md#id) is specified for the screen, `popTo` will match the screen by id instead of name.
1547+
15461548
#### `popToTop`
15471549
15481550
Pops all of the screens in the stack except the first one and navigates to it.

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,9 @@ In a stack navigator ([stack](stack-navigator.md) or [native stack](native-stack
106106
107107
- If you're already on a screen with the same name, it will update its params and not push a new screen.
108108
- If you're on a different screen, it will push the new screen onto the stack.
109-
- If the [`getId`](screen.md#id) prop is specified, and another screen in the stack has the same ID, it will bring that screen to focus and update its params instead.
109+
- If the [`getId`](screen.md#id) prop is specified, it's treated similarly to the name,
110+
- If you're already on a screen with the same id, it will update its params and not push a new screen.
111+
- If you're on a different screen, it will push the new screen onto the stack.
110112

111113
<details>
112114
<summary>Advanced usage</summary>
@@ -116,10 +118,8 @@ The `navigate` action can also accepts an object as the argument with the follow
116118
- `name` - _string_ - A destination name of the screen in the current or a parent navigator
117119
- `params` - _object_ - Params to use for the destination route.
118120
- `merge` - _boolean_ - Whether params should be merged with the existing route params, or replace them (when navigating to an existing screen). Defaults to `false`.
119-
- `pop` - _boolean_ - Whether screens should be popped to navigate to a matching screen in the stack. Defaults to `false`.
120-
- `path` - _string_ - The path (from deep link or universal link) to associate with the screen.
121-
122-
This is primarily used internally to associate a path with a screen when it's from a URL.
121+
- `pop` - _boolean_ - Whether screens should be popped to navigate to a matching screen (by name or id if `getId` is specified) in the stack. Defaults to `false`.
122+
- `path` - _string_ - The path (from deep link or universal link) to associate with the screen. This is primarily used internally to associate a path with a screen when it's from a URL.
123123
124124
</details>
125125

versioned_docs/version-8.x/navigation-object.md

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -74,13 +74,13 @@ The vast majority of your interactions with the `navigation` object will involve
7474

7575
The `navigate` method lets us navigate to another screen in your app. It takes the following arguments:
7676

77-
`navigation.navigate(name, params)`
77+
`navigation.navigate(name, params, options)`
7878

7979
- `name` - _string_ - A destination name of the screen in the current or a parent navigator.
8080
- `params` - _object_ - Params to use for the destination route.
8181
- `options` - Options object containing the following properties:
8282
- `merge` - _boolean_ - Whether params should be merged with the existing route params, or replace them (when navigating to an existing screen). Defaults to `false`.
83-
- `pop` - _boolean_ - Whether screens should be popped to navigate to a matching screen in the stack. Defaults to `false`.
83+
- `pop` - _boolean_ - Whether screens should be popped to navigate to a matching screen (by name or id if `getId` is specified) in the stack. Defaults to `false`.
8484

8585
<Tabs groupId="config" queryString="config">
8686
<TabItem value="static" label="Static" default>
@@ -249,8 +249,9 @@ In a stack navigator ([stack](stack-navigator.md) or [native stack](native-stack
249249

250250
- If you're already on a screen with the same name, it will update its params and not push a new screen.
251251
- If you're on a different screen, it will push the new screen onto the stack.
252-
- If the [`getId`](screen.md#id) prop is specified, and another screen in the stack has the same ID, it will bring that screen to focus and update its params instead.
253-
- If none of the above conditions match, it'll push a new screen to the stack.
252+
- If the [`getId`](screen.md#id) prop is specified, it's treated similarly to the name,
253+
- If you're already on a screen with the same id, it will update its params and not push a new screen.
254+
- If you're on a different screen, it will push the new screen onto the stack.
254255

255256
In a tab or drawer navigator, calling `navigate` will switch to the relevant screen if it's not focused already and update the params of the screen.
256257

versioned_docs/version-8.x/screen.md

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,10 @@ const Stack = createNativeStackNavigator({
210210

211211
### ID
212212

213-
A screen can have an ID to identify it uniquely. This is useful when you want to ensure that the screen with the same ID doesn't appear multiple times in the stack.
213+
A screen can have an ID to identify it. The ID is used differently based on the navigator type.
214+
215+
- In a stack navigator, the ID is treated similarly to the name.
216+
- In a tab or drawer navigator, the screen will remount if the ID changes.
214217

215218
This can be done by specifying the `getId` callback. It receives an object with the route params:
216219

@@ -244,22 +247,7 @@ const Stack = createStackNavigator({
244247
</TabItem>
245248
</Tabs>
246249

247-
In the above example, `params.userId` is used as an ID for the `Profile` screen with `getId`. This changes how the navigation works to ensure that the screen with the same ID appears only once in the stack.
248-
249-
Let's say you have a stack with the history `Home > Profile (userId: bob) > Settings`, consider following scenarios:
250-
251-
- You call `navigate(Profile, { userId: 'bob' })`:
252-
The resulting screens will be `Home > Settings > Profile (userId: bob)` since the existing `Profile` screen matches the ID.
253-
- You call `navigate(Profile, { userId: 'alice' })`:
254-
The resulting screens will be `Home > Profile (userId: bob) > Settings > Profile (userId: alice)` since it'll add a new `Profile` screen as no matching screen was found.
255-
256-
If `getId` is specified in a tab or drawer navigator, the screen will remount if the ID changes.
257-
258-
:::warning
259-
260-
If you're using [`@react-navigation/native-stack`](native-stack-navigator.md), it doesn't work correctly with the `getId` callback. So it's recommended to avoid using it in that case.
261-
262-
:::
250+
In the above example, `params.userId` is used as an ID for the `Profile` screen with `getId`. So if you navigate to `Profile` with the same `userId`, it'll update the params of the existing screen instead of pushing a new one. If you navigate to `Profile` with a different `userId`, it'll push a new screen onto the stack.
263251

264252
### Component
265253

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,8 @@ The method accepts the following arguments:
287287
- `options` - Options object containing the following properties:
288288
- `merge` - _boolean_ - Whether params should be merged with the existing route params, or replace them (when navigating to an existing screen). Defaults to `false`.
289289
290+
If [`getId`](screen.md#id) is specified for the screen, `popTo` will match the screen by id instead of name.
291+
290292
```js name="Stack actions popTo" snack static2dynamic
291293
import * as React from 'react';
292294
import { View, Text } from 'react-native';

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -388,6 +388,14 @@ This way you have more control over how params are updated in tab and drawer nav
388388

389389
See [`setParams` action docs](navigation-actions.md#setparams) for more details.
390390

391+
#### `getId` no longer re-arranges screens in the stack to make the screen unique
392+
393+
Previously, the ID returned by `getId` was treated as a unique identifier. When using `navigate` or `push` to go to a route with an ID that already existed in the stack, the stack would rearrange to bring the existing route to the front. However, this resulted in broken behavior with Native Stack Navigator.
394+
395+
Since this was unusable, and resulted in hard to debug issues, we have changed the behavior of `getId` to treat it more like the route name - that is, it will match routes by ID without rearranging the stack. If you navigate to a route with an existing ID and `pop: true`, it will pop back to the matching route instead of moving it to the top.
396+
397+
If you were relying on the previous behavior and are not using Native Stack Navigator, you can use the [`router` prop](#navigators-now-accept-a-router-prop) on the navigator to customize how each action updates the state, and implement the previous behavior.
398+
391399
#### Navigators no longer use `InteractionManager`
392400

393401
Previously, various navigators used `InteractionManager` to mark when animations and gestures were in progress. This was primarily used to defer code that should run after transitions, such as loading data or rendering heavy components.

0 commit comments

Comments
 (0)