Skip to content

Commit eb70cae

Browse files
committed
Update docs for typed hooks for dynamic config
1 parent db770d4 commit eb70cae

11 files changed

Lines changed: 404 additions & 286 deletions

versioned_docs/version-7.x/from-expo-router.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ React Navigation starts with the navigation tree:
107107

108108
type RootStackType = typeof RootStack;
109109

110-
declare module '@react-navigation/core' {
110+
declare module '@react-navigation/native' {
111111
interface RootNavigator extends RootStackType {}
112112
}
113113
```
@@ -500,7 +500,7 @@ export default function App() {
500500

501501
type RootStackType = typeof RootStack;
502502

503-
declare module '@react-navigation/core' {
503+
declare module '@react-navigation/native' {
504504
interface RootNavigator extends RootStackType {}
505505
}
506506
```

versioned_docs/version-7.x/static-vs-dynamic.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -618,7 +618,7 @@ const RootStack = createNativeStackNavigator({
618618

619619
type RootStackType = typeof RootStack;
620620

621-
declare module '@react-navigation/core' {
621+
declare module '@react-navigation/native' {
622622
interface RootNavigator extends RootStackType {}
623623
}
624624
```

versioned_docs/version-7.x/typescript.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ There are 2 steps to configure TypeScript with the static API:
3535
}
3636
```
3737

38-
2. Declare a module augmentation for `@react-navigation/core` and extend the `RootNavigator` interface with the type of your root navigator.
38+
2. Declare a module augmentation for `@react-navigation/native` and extend the `RootNavigator` interface with the type of your root navigator.
3939

4040
```ts
4141
const HomeTabs = createBottomTabNavigator({
@@ -55,7 +55,7 @@ There are 2 steps to configure TypeScript with the static API:
5555
type RootStackType = typeof RootStack;
5656

5757
// highlight-start
58-
declare module '@react-navigation/core' {
58+
declare module '@react-navigation/native' {
5959
interface RootNavigator extends RootStackType {}
6060
}
6161
// highlight-end
@@ -501,14 +501,14 @@ const navigationRef =
501501

502502
Instead of manually annotating these APIs, you can specify the type of your root navigator, which will be used to infer the default types.
503503

504-
To do this, you can use module augmentation for `@react-navigation/core` and extend the `RootNavigator` interface with the type of your root navigator:
504+
To do this, you can use module augmentation for `@react-navigation/native` and extend the `RootNavigator` interface with the type of your root navigator:
505505

506506
```ts
507507
// highlight-next-line
508508
type RootStackType = typeof RootStack;
509509

510510
// highlight-start
511-
declare module '@react-navigation/core' {
511+
declare module '@react-navigation/native' {
512512
interface RootNavigator extends RootStackType {}
513513
}
514514
// highlight-end
@@ -571,7 +571,7 @@ function App() {
571571

572572
type RootStackType = typeof RootStack;
573573

574-
declare module '@react-navigation/core' {
574+
declare module '@react-navigation/native' {
575575
interface RootNavigator extends RootStackType {}
576576
}
577577
```

versioned_docs/version-8.x/combine-static-with-dynamic.md

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -44,14 +44,14 @@ Here, `FeedScreen` is a component that renders a bottom tab navigator and is def
4444
```js
4545
import { createBottomTabNavigator } from '@react-navigation/bottom-tabs';
4646

47-
const Tab = createBottomTabNavigator();
47+
const FeedTabs = createBottomTabNavigator();
4848

4949
function FeedScreen() {
5050
return (
51-
<Tab.Navigator>
52-
<Tab.Screen name="Latest" component={LatestScreen} />
53-
<Tab.Screen name="Popular" component={PopularScreen} />
54-
</Tab.Navigator>
51+
<FeedTabs.Navigator>
52+
<FeedTabs.Screen name="Latest" component={LatestScreen} />
53+
<FeedTabs.Screen name="Popular" component={PopularScreen} />
54+
</FeedTabs.Navigator>
5555
);
5656
}
5757
```
@@ -96,14 +96,17 @@ import {
9696
StaticScreenProps,
9797
NavigatorScreenParams,
9898
} from '@react-navigation/native';
99+
import { createBottomTabNavigator } from '@react-navigation/bottom-tabs';
99100

100-
type FeedParamList = {
101+
type FeedTabsParamList = {
101102
Latest: undefined;
102103
Popular: undefined;
103104
};
104105

106+
const FeedTabs = createBottomTabNavigator<FeedTabsParamList>();
107+
105108
// highlight-next-line
106-
type Props = StaticScreenProps<NavigatorScreenParams<FeedParamList>>;
109+
type Props = StaticScreenProps<NavigatorScreenParams<typeof FeedTabs>>;
107110

108111
// highlight-next-line
109112
function FeedScreen(_: Props) {
@@ -114,10 +117,13 @@ function FeedScreen(_: Props) {
114117
In the above snippet:
115118

116119
1. We first define the param list type for screens in the navigator that defines params for each screen
117-
2. Then we use the `NavigatorScreenParams` type to get the type of route's `params` which will include types for the nested screens
118-
3. Finally, we use the type of `params` with `StaticScreenProps` to define the type of the screen component
120+
2. Then we create the dynamic navigator with that param list
121+
3. Then we use the `NavigatorScreenParams` type with `typeof FeedTabs` to get the type of route's `params` which will include types for the nested screens and the concrete navigator type
122+
4. Finally, we use the type of `params` with `StaticScreenProps` to define the type of the screen component
119123

120-
This is based on how we'd define the type for a screen with a nested navigator with the dynamic API. See [Type checking screens and params in nested navigator](typescript.md#type-checking-screens-and-params-in-nested-navigator).
124+
Using `typeof FeedTabs` lets typed hooks such as `useNavigation('Latest')`, `useRoute('Latest')`, and `useNavigationState('Latest', selector)` infer the screens inside the nested tab navigator.
125+
126+
This is based on how we'd define the type for a screen with a nested navigator with the dynamic API. See [Specifying param types for screens](typescript.md#specifying-param-types-for-screens-dynamic).
121127

122128
## Dynamic root navigator, static nested navigator
123129

@@ -168,21 +174,15 @@ To use the `FeedTabs` navigator for the `Feed` screen, we need to get a componen
168174
const FeedScreen = FeedTabs.getComponent();
169175
```
170176

171-
In addition, we can generate the TypeScript types for the `FeedTabs` navigator and use it in the types of `RootStack` without needing to write them manually:
177+
In addition, we can use the type of the `FeedTabs` navigator in the types of `RootStack`:
172178

173179
```tsx
174-
import {
175-
StaticParamList,
176-
NavigatorScreenParams,
177-
} from '@react-navigation/native';
178-
179-
// highlight-next-line
180-
type FeedTabsParamList = StaticParamList<typeof FeedTabs>;
180+
import { NavigatorScreenParams } from '@react-navigation/native';
181181

182182
type RootStackParamList = {
183183
Home: undefined;
184184
// highlight-next-line
185-
Feed: NavigatorScreenParams<FeedTabsParamList>;
185+
Feed: NavigatorScreenParams<typeof FeedTabs>;
186186
};
187187
```
188188

versioned_docs/version-8.x/from-expo-router.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ React Navigation starts with the navigation tree:
108108

109109
type RootStackType = typeof RootStack;
110110

111-
declare module '@react-navigation/core' {
111+
declare module '@react-navigation/native' {
112112
interface RootNavigator extends RootStackType {}
113113
}
114114
```
@@ -503,7 +503,7 @@ export default function App() {
503503

504504
type RootStackType = typeof RootStack;
505505

506-
declare module '@react-navigation/core' {
506+
declare module '@react-navigation/native' {
507507
interface RootNavigator extends RootStackType {}
508508
}
509509
```

versioned_docs/version-8.x/hello-react-navigation.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -346,7 +346,7 @@ If you're using TypeScript, you need to tell React Navigation about your root na
346346
```ts
347347
type RootStackType = typeof RootStack;
348348

349-
declare module '@react-navigation/core' {
349+
declare module '@react-navigation/native' {
350350
interface RootNavigator extends RootStackType {}
351351
}
352352
```

versioned_docs/version-8.x/navigating-without-navigation-prop.md

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,7 @@ export default function App() {
6060

6161
In the next step, we define `RootNavigation`, which is a simple module with functions that dispatch user-defined navigation actions.
6262

63-
```js
64-
// RootNavigation.js
65-
63+
```js title="RootNavigation.js"
6664
import { createNavigationContainerRef } from '@react-navigation/native';
6765

6866
export const navigationRef = createNavigationContainerRef();

versioned_docs/version-8.x/static-vs-dynamic.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -579,7 +579,7 @@ const RootStack = createNativeStackNavigator<RootStackParamList>();
579579

580580
type RootStackType = typeof RootStack;
581581

582-
declare module '@react-navigation/core' {
582+
declare module '@react-navigation/native' {
583583
interface RootNavigator extends RootStackType {}
584584
}
585585

@@ -610,7 +610,7 @@ const RootStack = createNativeStackNavigator({
610610

611611
type RootStackType = typeof RootStack;
612612

613-
declare module '@react-navigation/core' {
613+
declare module '@react-navigation/native' {
614614
interface RootNavigator extends RootStackType {}
615615
}
616616
```

versioned_docs/version-8.x/troubleshooting.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -467,8 +467,8 @@ This usually means that TypeScript doesn't know the route names and params for y
467467
468468
Ensure the following:
469469
470-
- You have configured global types for your navigator. See [specifying root navigator type](typescript.md#specify-the-root-navigators-type).
471-
- If you are using the dynamic API, you have provided a param list as a generic (e.g. `createStackNavigator<MyParamList>()`) and that your screen names match the keys in that param list. See [type checking the navigator](typescript.md#typechecking-the-navigator).
470+
- You have configured global types for your navigator. See [specifying the root navigator's type](typescript.md#specifying-the-root-navigators-type).
471+
- If you are using the dynamic API, you have provided a param list as a generic (e.g. `createStackNavigator<MyParamList>()`) and that your screen names match the keys in that param list. See [specifying param types for screens](typescript.md#specifying-param-types-for-screens-dynamic).
472472
473473
## Screens are unmounting/remounting during navigation
474474

0 commit comments

Comments
 (0)