Skip to content

Commit 149cf34

Browse files
committed
Document UNSTABLE_routeNamesChangeBehavior in API references
1 parent 1caf630 commit 149cf34

File tree

2 files changed

+79
-1
lines changed

2 files changed

+79
-1
lines changed

versioned_docs/version-7.x/auth-flow.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -949,7 +949,7 @@ Example scenario:
949949
- The app shows the `SignIn` screen.
950950
- After the user signs in, you want to navigate them to the `Profile` screen.
951951

952-
To achieve this, you can pass `UNSTABLE_routeNamesChangeBehavior="lastUnhandled"`:
952+
To achieve this, you can set [`UNSTABLE_routeNamesChangeBehavior`](navigator.md#routenameschangebehavior) to `"lastUnhandled"`:
953953

954954
:::warning
955955

versioned_docs/version-7.x/navigator.md

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -383,3 +383,81 @@ The function passed to `UNSTABLE_router` **must be a pure function and cannot re
383383
The overrides object is shallow merged with the original router. So you don't need to specify all properties of the router, only the ones you want to override.
384384

385385
See [custom routers](custom-routers.md) for more details on routers.
386+
387+
### Route names change behavior
388+
389+
:::warning
390+
391+
This API is experimental and may change in a minor release.
392+
393+
:::
394+
395+
When the list of available routes in a navigator changes dynamically, e.g. based on conditional rendering, looping over data from an API etc., the navigator needs to update the [navigation state](navigation-state.md) according to the new list of routes.
396+
397+
By default, it works as follows:
398+
399+
- Any routes not present in the new available list of routes are removed from the navigation state
400+
- If the currently focused route is still present in the new available list of routes, it remains focused.
401+
- If the currently focused route has been removed, but the navigation state has other routes that are present in the new available list, the first route in from the list of rendered routes becomes focused.
402+
- If none of the routes in the navigation state are present in the new available list of routes, one of the following things can happen based on the `UNSTABLE_routeNamesChangeBehavior` prop:
403+
- `'firstMatch'` - The first route defined in the new list of routes becomes focused. This is the default behavior based on [`getStateForRouteNamesChange`](custom-routers.md) in the router.
404+
- `'lastUnhandled'` - The last state that was unhandled due to conditional rendering is restored.
405+
406+
Example cases where state might have been unhandled:
407+
408+
- Opened a deep link to a screen, but a login screen was shown.
409+
- Navigated to a screen containing a navigator, but a different screen was shown.
410+
- Reset the navigator to a state with different routes not matching the available list of routes.
411+
412+
In these cases, specifying `'lastUnhandled'` will reuse the unhandled state if present. If there's no unhandled state, it will fallback to `'firstMatch'` behavior.
413+
414+
Caveats:
415+
416+
- Direct navigation is only handled for `NAVIGATE` actions.
417+
- Unhandled state is restored only if the current state becomes invalid, i.e. it doesn't contain any currently defined screens.
418+
419+
Example usage:
420+
421+
<Tabs groupId="config" queryString="config">
422+
<TabItem value="static" label="Static" default>
423+
424+
```js
425+
const RootStack = createNativeStackNavigator({
426+
// highlight-next-line
427+
UNSTABLE_routeNamesChangeBehavior: 'lastUnhandled',
428+
screens: {
429+
Home: {
430+
if: useIsSignedIn,
431+
screen: HomeScreen,
432+
},
433+
SignIn: {
434+
if: useIsSignedOut,
435+
screen: SignInScreen,
436+
options: {
437+
title: 'Sign in',
438+
},
439+
},
440+
},
441+
});
442+
```
443+
444+
</TabItem>
445+
<TabItem value="dynamic" label="Dynamic">
446+
447+
```js
448+
<Stack.Navigator
449+
// highlight-next-line
450+
UNSTABLE_routeNamesChangeBehavior="lastUnhandled"
451+
>
452+
{isSignedIn ? (
453+
<Stack.Screen name="Home" component={HomeScreen} />
454+
) : (
455+
<Stack.Screen name="SignIn" component={SignInScreen} />
456+
)}
457+
</Stack.Navigator>
458+
```
459+
460+
</TabItem>
461+
</Tabs>
462+
463+
The most common use case for this is to [show the correct screen based on authentication based on deep link](auth-flow.md#handling-deep-links-after-auth).

0 commit comments

Comments
 (0)