Skip to content

Commit 6518235

Browse files
committed
Align custom navigator examples with latest patterns
1 parent d36f816 commit 6518235

2 files changed

Lines changed: 46 additions & 33 deletions

File tree

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

Lines changed: 25 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,6 @@ title: Custom navigators
44
sidebar_label: Custom navigators
55
---
66

7-
import Tabs from '@theme/Tabs';
8-
import TabItem from '@theme/TabItem';
9-
107
In essence, a navigator is a React component that takes a set of [screens](screen.md) and options, and renders them based on its [navigation state](navigation-state.md), generally with additional UI such as [headers](headers.md), [tab bars](bottom-tab-navigator.md), or [drawers](drawer-navigator.md).
118

129
React Navigation provides a few built-in navigators, but they might not always fit your needs if you want a very custom behavior or UI. In such cases, you can build your own custom navigators using React Navigation's APIs.
@@ -170,15 +167,12 @@ So custom navigators need to wrap the navigator component in `createNavigatorFac
170167
Example:
171168
172169
```js
173-
import {
174-
useNavigationBuilder,
175-
createNavigatorFactory,
176-
} from '@react-navigation/native';
170+
import { createNavigatorFactory } from '@react-navigation/native';
177171

178172
// ...
179173

180174
export function createMyNavigator(config) {
181-
return createNavigatorFactory(TabNavigator)(config);
175+
return createNavigatorFactory(MyNavigator)(config);
182176
}
183177
```
184178
@@ -216,6 +210,7 @@ To type-check navigators, we need to provide few types:
216210
- Type of supported screen options
217211
- A map of event types emitted by the navigator
218212
- The type of the navigation object for each screen
213+
- The type of the props for each screen
219214
220215
We also need to export a function to create the navigator configuration with proper types.
221216
@@ -237,6 +232,7 @@ import {
237232
type DefaultNavigatorOptions,
238233
type NavigatorTypeBagBase,
239234
type ParamListBase,
235+
type RouteProp,
240236
type StaticConfig,
241237
type TabActionHelpers,
242238
type TabNavigationState,
@@ -284,6 +280,16 @@ export type MyNavigationProp<
284280
> &
285281
TabActionHelpers<ParamList>;
286282

283+
// The type of the props for each screen
284+
export type MyNavigatorScreenProps<
285+
ParamList extends ParamListBase,
286+
RouteName extends keyof ParamList = keyof ParamList,
287+
NavigatorID extends string | undefined = undefined,
288+
> = {
289+
navigation: MyNavigationProp<ParamList, RouteName, NavigatorID>;
290+
route: RouteProp<ParamList, RouteName>;
291+
};
292+
287293
// The props accepted by the component is a combination of 3 things
288294
type Props = DefaultNavigatorOptions<
289295
ParamListBase,
@@ -356,22 +362,10 @@ function TabNavigator({ tabBarStyle, contentStyle, ...rest }: Props) {
356362
);
357363
}
358364

359-
// Types required for type-checking the navigator
360-
type MyTabTypeBag<ParamList extends {}> = {
361-
ParamList: ParamList;
362-
State: TabNavigationState<ParamList>;
363-
ScreenOptions: MyNavigationOptions;
364-
EventMap: MyNavigationEventMap;
365-
NavigationList: {
366-
[RouteName in keyof ParamList]: MyNavigationProp<ParamList, RouteName>;
367-
};
368-
Navigator: typeof TabNavigator;
369-
};
370-
371365
// The factory function with overloads for static and dynamic configuration
372366
export function createMyNavigator<
373367
const ParamList extends ParamListBase,
374-
const NavigatorID extends string | undefined = undefined,
368+
const NavigatorID extends string | undefined = string | undefined,
375369
const TypeBag extends NavigatorTypeBagBase = {
376370
ParamList: ParamList;
377371
NavigatorID: NavigatorID;
@@ -407,24 +401,30 @@ import {
407401
import { BottomTabView } from '@react-navigation/bottom-tabs';
408402

409403
function MyBottomTabNavigator({
404+
id,
410405
initialRouteName,
406+
backBehavior,
407+
UNSTABLE_routeNamesChangeBehavior,
411408
children,
412409
layout,
413410
screenListeners,
414411
screenOptions,
415412
screenLayout,
416-
backBehavior,
413+
UNSTABLE_router,
417414
...rest
418415
}) {
419416
const { state, descriptors, navigation, NavigationContent } =
420417
useNavigationBuilder(TabRouter, {
418+
id,
421419
initialRouteName,
420+
backBehavior,
421+
UNSTABLE_routeNamesChangeBehavior,
422422
children,
423423
layout,
424424
screenListeners,
425425
screenOptions,
426426
screenLayout,
427-
backBehavior,
427+
UNSTABLE_router,
428428
});
429429

430430
return (
@@ -455,12 +455,13 @@ const { state, descriptors, navigation, NavigationContent } =
455455
useNavigationBuilder(MyRouter, {
456456
id,
457457
initialRouteName,
458+
backBehavior,
459+
UNSTABLE_routeNamesChangeBehavior,
458460
children,
459461
layout,
460462
screenListeners,
461463
screenOptions,
462464
screenLayout,
463-
backBehavior,
464465
});
465466

466467
// ...

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

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ Example:
8989

9090
```js
9191
import * as React from 'react';
92-
import { Text, Pressable, View, StyleSheet } from 'react-native';
92+
import { Text, Pressable, View } from 'react-native';
9393
import {
9494
useNavigationBuilder,
9595
TabRouter,
@@ -167,10 +167,7 @@ So custom navigators need to wrap the navigator component in `createNavigatorFac
167167
Example:
168168
169169
```js
170-
import {
171-
useNavigationBuilder,
172-
createNavigatorFactory,
173-
} from '@react-navigation/native';
170+
import { createNavigatorFactory } from '@react-navigation/native';
174171

175172
// ...
176173

@@ -249,6 +246,7 @@ To type-check navigators, we need to provide few types:
249246
- Type of supported screen options
250247
- A map of event types emitted by the navigator
251248
- The type of the navigation object for each screen
249+
- The type of the props for each screen
252250
253251
We also need to export functions to create the navigator and screen configurations with proper types.
254252
@@ -262,7 +260,6 @@ import {
262260
Pressable,
263261
type StyleProp,
264262
type ViewStyle,
265-
StyleSheet,
266263
} from 'react-native';
267264
import {
268265
createNavigatorFactory,
@@ -272,6 +269,7 @@ import {
272269
type NavigationProp,
273270
type NavigatorTypeBagBase,
274271
type ParamListBase,
272+
type RouteProp,
275273
type TabActionHelpers,
276274
type TabNavigationState,
277275
TabRouter,
@@ -314,6 +312,15 @@ export type MyNavigationProp<
314312
TabActionHelpers<ParamList>
315313
>;
316314

315+
// The type of the props for each screen
316+
export type MyNavigatorScreenProps<
317+
ParamList extends ParamListBase,
318+
RouteName extends keyof ParamList = keyof ParamList,
319+
> = {
320+
navigation: MyNavigationProp<ParamList, RouteName>;
321+
route: RouteProp<ParamList, RouteName>;
322+
};
323+
317324
// The props accepted by the component is a combination of 3 things
318325
type Props = DefaultNavigatorOptions<
319326
ParamListBase,
@@ -421,23 +428,27 @@ import { BottomTabView } from '@react-navigation/bottom-tabs';
421428

422429
function MyBottomTabNavigator({
423430
initialRouteName,
431+
backBehavior,
432+
routeNamesChangeBehavior,
424433
children,
425434
layout,
426435
screenListeners,
427436
screenOptions,
428437
screenLayout,
429-
backBehavior,
438+
router,
430439
...rest
431440
}) {
432441
const { state, descriptors, navigation, NavigationContent } =
433442
useNavigationBuilder(TabRouter, {
434443
initialRouteName,
444+
backBehavior,
445+
routeNamesChangeBehavior,
435446
children,
436447
layout,
437448
screenListeners,
438449
screenOptions,
439450
screenLayout,
440-
backBehavior,
451+
router,
441452
});
442453

443454
return (
@@ -468,12 +479,13 @@ import MyRouter from './MyRouter';
468479
const { state, descriptors, navigation, NavigationContent } =
469480
useNavigationBuilder(MyRouter, {
470481
initialRouteName,
482+
backBehavior,
483+
routeNamesChangeBehavior,
471484
children,
472485
layout,
473486
screenListeners,
474487
screenOptions,
475488
screenLayout,
476-
backBehavior,
477489
});
478490

479491
// ...

0 commit comments

Comments
 (0)