You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@@ -51,13 +51,130 @@ Your package exports can point to those entry points:
51
51
}
52
52
```
53
53
54
+
To get React Navigation types, you can add `@react-navigation/native` as a `devDependency` and an optional `peerDependency`:
55
+
56
+
```json
57
+
{
58
+
"devDependencies": {
59
+
"@react-navigation/native": "^7.0.0"
60
+
},
61
+
"peerDependencies": {
62
+
"@react-navigation/native": ">= 7.0.0"
63
+
},
64
+
"peerDependenciesMeta": {
65
+
"@react-navigation/native": {
66
+
"optional": true
67
+
}
68
+
}
69
+
}
70
+
```
71
+
54
72
## Standard navigator implementation
55
73
56
74
The standard navigator file should export the navigator object created with `createStandardNavigator`. This file shouldn't import React Navigation or Expo Router APIs.
57
75
58
76
To create a standard navigator, use the `createStandardNavigator` function from `standard-navigation`, and pass it a component that renders the desired UI.
// render the navigator UI using the state and descriptors
99
+
// use actions to perform navigation and emitter to emit events
100
+
});
101
+
```
102
+
103
+
The object returned by `createStandardNavigator` can then be used in the React Navigation and Expo Router entry points to create the navigators for each library.
104
+
105
+
The `createStandardNavigator` function accepts three generic arguments:
106
+
107
+
-**`MyTabOptions`**
108
+
109
+
The type of the options available for each screen. It's a record of option names to their types.
110
+
e.g.:
111
+
112
+
```ts
113
+
typeMyTabOptions= {
114
+
title?:string;
115
+
};
116
+
```
117
+
118
+
-**`MyTabEventMap`**
119
+
120
+
The type of the events that can be emitted by the navigator. It's a mapping of event names to event data and whether the event can be prevented.
121
+
e.g.:
122
+
123
+
```ts
124
+
typeMyTabEventMap= {
125
+
tabPress: {
126
+
data: { isAlreadyFocused:boolean };
127
+
canPreventDefault:true;
128
+
};
129
+
};
130
+
```
131
+
132
+
-**`MyTabNavigatorProps`**
133
+
134
+
The type of any additional props accepted by the navigator.
135
+
136
+
The callback receives `state`, `descriptors`, `actions`, and `emitter` from the navigation library integration:
137
+
138
+
-**`state`**
139
+
140
+
The state object for the navigator. Includes:
141
+
-`state.index`: The index of the currently focused route.
142
+
-`state.routes`: An array of route objects, each with `key`, `name`, `params` and `href` properties.
143
+
144
+
For stack navigators, `state.routes` array contains the history of visited screens until `state.index`, and the route objects after `state.index` represent [preloaded](navigation-actions.md#preload) routes.
145
+
146
+
-**`descriptors`**
147
+
148
+
An object mapping route keys to their descriptors. `descriptors[route.key]` will give you the descriptor for a specific route, which includes:
149
+
-`descriptors[route.key].options`: The options for the route.
150
+
-`descriptors[route.key].render()`: Function that returns the react element to render for the route.
151
+
152
+
-**`actions`**
153
+
154
+
An object with functions to perform navigation actions. Available actions are:
155
+
-`actions.navigate(name, params)`: Navigate to a route with the given name and params.
156
+
-`actions.back()`: Go back to the previous route in history.
157
+
158
+
-**`emitter`**
159
+
160
+
An object with a function to emit events from the navigator. The `emitter.emit(...)` function accepts an object with the following properties:
161
+
-`type`: The name of the event to emit, one of the keys in the event map type.
162
+
-`target`: The key of the route that is the target of the event, i.e. the route that can listen for the event.
163
+
-`data`: An object with any additional data to include with the event.
164
+
-`canPreventDefault`: A boolean indicating whether listeners can call `event.preventDefault()` to prevent the default behavior associated with the event.
165
+
166
+
Example:
167
+
168
+
```ts
169
+
emitter.emit({
170
+
type: 'tabPress',
171
+
target: route.key,
172
+
canPreventDefault: true,
173
+
data: { isAlreadyFocused: isFocused },
174
+
});
175
+
```
176
+
177
+
A basic implementation of a tab navigator could look like this:
For stack navigators, `state.routes` array contains the history of visited screens until `state.index`, and the route objects after `state.index` represent [preloaded](navigation-actions.md#preload) routes.
278
+
The `createStandardNavigationFactories` function accepts two generic arguments:
157
279
158
-
:::
280
+
- The type bag for the navigator (e.g. `MyTabTypeBag`), which includes the state, action helpers, screen options, event map, and router options types.
281
+
- The type of any additional props accepted by the navigator (e.g. `MyTabNavigatorProps`).
159
282
160
-
## React Navigation entry point
283
+
It accepts 3 arguments:
161
284
162
-
The React Navigation entry point should wrap the standard navigator with `createStandardNavigationFactories` from `@react-navigation/native`:
285
+
- The standard navigator component.
286
+
- The router factory function from React Navigation (e.g. `TabRouter`, `StackRouter`, etc.).
287
+
- An optional function to map `{ navigation, state }` to custom props for the navigator component, in case you need any specific state or action helpers not available in the standard ones.
288
+
289
+
It returns an object with `createNavigator` and `createScreen` functions that can be used to create the navigator and screens for React Navigation. These should be exported from the entry point.
290
+
291
+
Additionally, you can export custom navigation prop and screen prop types (e.g. `MyTabNavigationProp` and `MyTabScreenProps`) that can be used by consumers for type annotations.
292
+
293
+
A basic implementation of the React Navigation entry point could look like this:
163
294
164
295
```tsx title="src/react-navigation.tsx"
165
296
import {
@@ -221,21 +352,6 @@ export const {
221
352
);
222
353
```
223
354
224
-
The `createStandardNavigationFactories` function accepts two generic arguments:
225
-
226
-
- The type bag for the navigator (e.g. `MyTabTypeBag`), which includes the state, action helpers, screen options, event map, and router options types.
227
-
- The type of any additional props accepted by the navigator (e.g. `MyTabNavigatorProps`).
228
-
229
-
It accepts 3 arguments:
230
-
231
-
- The standard navigator component.
232
-
- The router factory function from React Navigation (e.g. `TabRouter`, `StackRouter`, etc.).
233
-
- An optional function to map `{ navigation, state }` to custom props for the navigator component, in case you need any specific state or action helpers not available in the standard ones.
234
-
235
-
It returns an object with `createNavigator` and `createScreen` functions that can be used to create the navigator and screens for React Navigation. These should be exported from the entry point.
236
-
237
-
Additionally, you can export custom navigation prop and screen prop types (e.g. `MyTabNavigationProp` and `MyTabScreenProps`) that can be used by consumers for type annotations.
238
-
239
355
Consumers can then use the React Navigation entry point:
0 commit comments