Skip to content

Commit 53a0b08

Browse files
committed
Update docs for getId
1 parent 10b4e56 commit 53a0b08

File tree

4 files changed

+16
-80
lines changed

4 files changed

+16
-80
lines changed

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,7 @@ In a stack navigator ([stack](stack-navigator.md) or [native stack](native-stack
282282
283283
- If you're already on a screen with the same name, it will update its params and not push a new screen.
284284
- If you're on a different screen, it will push the new screen onto the stack.
285-
- If the [`getId`](screen.md#id) prop is specified, and another screen in the stack has the same ID, it will navigate to that screen and update its params instead.
285+
- 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.
286286
287287
<details>
288288
<summary>Advanced usage</summary>

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

+2-37
Original file line numberDiff line numberDiff line change
@@ -248,40 +248,6 @@ In a stack navigator ([stack](stack-navigator.md) or [native stack](native-stack
248248
- 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.
249249
- If none of the above conditions match, it'll push a new screen to the stack.
250250

251-
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#id) prop.
252-
253-
For example, say you have specified a `getId` prop for `Profile` screen:
254-
255-
<Tabs groupId="config" queryString="config">
256-
<TabItem value="static" label="Static" default>
257-
258-
```js
259-
const Tabs = createBottomTabNavigator({
260-
screens: {
261-
Profile: {
262-
screen: ProfileScreen,
263-
getId: ({ params }) => params.userId,
264-
},
265-
},
266-
});
267-
```
268-
269-
</TabItem>
270-
<TabItem value="dynamic" label="Dynamic">
271-
272-
```js
273-
<Tab.Screen
274-
name={Profile}
275-
component={ProfileScreen}
276-
getId={({ params }) => params.userId}
277-
/>
278-
```
279-
280-
</TabItem>
281-
</Tabs>
282-
283-
Now, if you have a stack with the history `Home > Profile (userId: bob) > Settings` and you call `navigate(Profile, { userId: 'alice' })`, 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.
284-
285251
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.
286252

287253
### `navigateDeprecated`
@@ -304,9 +270,8 @@ It takes the following arguments:
304270
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:
305271

306272
- If you're already on a screen with the same name, it will update its params and not push a new screen.
307-
- If a screen with the same name already exists in the stack, it will pop all the screens after it to go back to the existing screen.
308-
- If the [`getId`](screen.md#id) prop is specified, and another screen in the stack has the same ID, it will pop any screens to navigate to that screen and update its params instead.
309-
- If none of the above conditions match, it'll push a new screen to the stack.
273+
- If you're on a different screen, it will push the new screen onto the stack.
274+
- 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.
310275

311276
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.
312277

versioned_docs/version-7.x/screen.md

+6-39
Original file line numberDiff line numberDiff line change
@@ -244,47 +244,14 @@ const Stack = createNativeStackNavigator({
244244
</TabItem>
245245
</Tabs>
246246

247-
By default, `navigate('ScreenName', params)` updates the current screen if the screen name matches, otherwise adds a new screen in a stack navigator. So if you're on `ScreenName` and navigate to `ScreenName` again, it won't add a new screen even if the params are different - it'll update the current screen with the new params instead:
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.
248248

249-
```js
250-
// Let's say you're on `Home` screen
251-
// Then you navigate to `Profile` screen with `userId: 1`
252-
navigation.navigate('Profile', { userId: 1 });
253-
254-
// Now the stack will have: `Home` -> `Profile` with `userId: 1`
255-
256-
// Then you navigate to `Profile` screen again with `userId: 2`
257-
navigation.navigate('Profile', { userId: 2 });
258-
259-
// The stack will now have: `Home` -> `Profile` with `userId: 2`
260-
```
261-
262-
If you specify `getId` and it doesn't return `undefined`, the screen is identified by both the screen name and the returned ID. That means that if you're on `ScreenName` and navigate to `ScreenName` again with different params - and return a different ID from the `getId` callback, it'll add a new screen to the stack:
263-
264-
```js
265-
// Let's say you're on `Home` screen
266-
// Then you navigate to `Profile` screen with `userId: 1`
267-
navigation.navigate('Profile', { userId: 1 });
268-
269-
// Now the stack will have: `Home` -> `Profile` with `userId: 1`
270-
271-
// Then you navigate to `Profile` screen again with `userId: 2`
272-
navigation.navigate('Profile', { userId: 2 });
273-
274-
// The stack will now have: `Home` -> `Profile` with `userId: 1` -> `Profile` with `userId: 2`
275-
```
276-
277-
The `getId` callback can also be used to ensure that the screen with the same ID doesn't appear multiple times in the stack:
278-
279-
```js
280-
// Let's say you have a stack with the screens: `Home` -> `Profile` with `userId: 1` -> `Settings`
281-
// Then you navigate to `Profile` screen with `userId: 1` again
282-
navigation.navigate('Profile', { userId: 1 });
283-
284-
// Now the stack will have: `Home` -> `Profile` with `userId: 1`
285-
```
249+
Let's say you have a stack with the history `Home > Profile (userId: bob) > Settings`, consider following scenarios:
286250

287-
In the above examples, `params.userId` is used as an ID, subsequent navigation to the screen with the same `userId` will navigate to the existing screen instead of adding a new one to the stack. If the navigation was with a different `userId`, then it'll add a new screen.
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.
288255

289256
If `getId` is specified in a tab or drawer navigator, the screen will remount if the ID changes.
290257

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

+7-3
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,12 @@ The methods now behave as follows:
5555

5656
See [`popTo`](stack-actions.md#popto) for more details.
5757

58-
To achieve a behavior similar to before with `navigate`, you can use the [`getId`](screen.md#id) prop in which case it'll go to the screen with the matching ID and push or pop screens accordingly.
58+
To achieve a behavior similar to before with `navigate`, you can use specify `pop: true` in the options:
59+
60+
```diff lang=js
61+
- navigation.navigate('PreviousScreen', { foo: 42 });
62+
+ navigation.navigate('PreviousScreen', { foo: 42 }, { pop: true });
63+
```
5964

6065
To help with the migration, we have added a new method called `navigateDeprecated` which will behave like the old `navigate` method. You can replace your current `navigate` calls with [`navigateDeprecated`](navigation-object.md#navigatedeprecated) to gradually migrate to the new behavior:
6166

@@ -80,8 +85,7 @@ It's problematic since:
8085
- None of the other actions support such usage.
8186
- Specifying a `key` is not type-safe, making it easy to cause bugs.
8287
83-
In React Navigation 5, we added the [`getId`](screen.md#id) prop which can be used for similar use cases - and gives users full control since they provide the ID and it's not autogenerated by the
84-
library.
88+
In React Navigation 5, we added the [`getId`](screen.md#id) prop which can be used for similar use cases - and gives users full control since they provide the ID and it's not autogenerated by the library.
8589

8690
So the `key` option is now being removed from the `navigate` action.
8791

0 commit comments

Comments
 (0)