Skip to content

Commit 1caf630

Browse files
committed
Document UNSTABLE_router prop
1 parent a9bc409 commit 1caf630

File tree

4 files changed

+89
-3
lines changed

4 files changed

+89
-3
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -953,7 +953,7 @@ To achieve this, you can pass `UNSTABLE_routeNamesChangeBehavior="lastUnhandled"
953953

954954
:::warning
955955

956-
This option is experimental and may change in a minor release.
956+
This API is experimental and may change in a minor release.
957957

958958
:::
959959

versioned_docs/version-7.x/custom-navigators.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -438,3 +438,14 @@ const { state, descriptors, navigation, NavigationContent } =
438438

439439
// ...
440440
```
441+
442+
:::note
443+
444+
Customizing built-in navigators like this is an advanced use case and generally not necessary. Consider alternatives such as:
445+
446+
- [`layout`](navigator.md#layout) prop on navigators to add a wrapper around the navigator
447+
- [`UNSTABLE_router`](navigator.md#router) prop on navigators to customize the router behavior
448+
449+
Also refer to the navigator's documentation to see if any existing API meets your needs.
450+
451+
:::

versioned_docs/version-7.x/custom-routers.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -150,9 +150,10 @@ The library ships with a few standard routers:
150150

151151
## Customizing Routers
152152

153-
You can reuse a router and override the router functions as per your needs, such as customizing how existing actions are handled, adding additional actions etc.
153+
There are two main ways to customize routers:
154154

155-
See [custom navigators](custom-navigators.md) for details on how to override the router with a custom router in an existing navigator.
155+
- Override an existing router with the [`UNSTABLE_router`](navigator.md#router) prop on navigators
156+
- Customized navigators with a custom router, see [extending navigators](custom-navigators.md#extending-navigators)
156157

157158
### Custom Navigation Actions
158159

versioned_docs/version-7.x/navigator.md

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -309,3 +309,77 @@ function MyStack() {
309309

310310
</TabItem>
311311
</Tabs>
312+
313+
### Router
314+
315+
:::warning
316+
317+
This API is experimental and may change in a minor release.
318+
319+
:::
320+
321+
Routers can be customized with the `UNSTABLE_router` prop on navigator to override how navigation actions are handled.
322+
323+
It takes a function that receives the original router and returns an object with overrides:
324+
325+
<Tabs groupId="config" queryString="config">
326+
<TabItem value="static" label="Static" default>
327+
328+
```js
329+
const MyStack = createNativeStackNavigator({
330+
// highlight-start
331+
UNSTABLE_router: (original) => ({
332+
getStateForAction(state, action) {
333+
if (action.type === 'SOME_ACTION') {
334+
// Custom logic
335+
}
336+
337+
// Fallback to original behavior
338+
return original.getStateForAction(state, action);
339+
},
340+
}),
341+
// highlight-end
342+
screens: {
343+
Home: HomeScreen,
344+
Profile: ProfileScreen,
345+
},
346+
});
347+
```
348+
349+
</TabItem>
350+
<TabItem value="dynamic" label="Dynamic">
351+
352+
```js
353+
const Stack = createNativeStackNavigator();
354+
355+
function MyStack() {
356+
return (
357+
<Stack.Navigator
358+
// highlight-start
359+
UNSTABLE_router={(original) => ({
360+
getStateForAction(state, action) {
361+
if (action.type === 'SOME_ACTION') {
362+
// Custom logic
363+
}
364+
365+
// Fallback to original behavior
366+
return original.getStateForAction(state, action);
367+
},
368+
})}
369+
// highlight-end
370+
>
371+
<Stack.Screen name="Home" component={HomeScreen} />
372+
<Stack.Screen name="Profile" component={ProfileScreen} />
373+
</Stack.Navigator>
374+
);
375+
}
376+
```
377+
378+
</TabItem>
379+
</Tabs>
380+
381+
The function passed to `UNSTABLE_router` **must be a pure function and cannot reference outside dynamic variables**.
382+
383+
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.
384+
385+
See [custom routers](custom-routers.md) for more details on routers.

0 commit comments

Comments
 (0)