@react-navigation/core 6.1.1 → 6.2.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (48) hide show
  1. package/lib/commonjs/SceneView.js.map +1 -1
  2. package/lib/commonjs/types.js.map +1 -1
  3. package/lib/commonjs/useChildListeners.js +6 -3
  4. package/lib/commonjs/useChildListeners.js.map +1 -1
  5. package/lib/commonjs/useDescriptors.js.map +1 -1
  6. package/lib/commonjs/useEventEmitter.js +12 -2
  7. package/lib/commonjs/useEventEmitter.js.map +1 -1
  8. package/lib/commonjs/useKeyedChildListeners.js +2 -0
  9. package/lib/commonjs/useKeyedChildListeners.js.map +1 -1
  10. package/lib/commonjs/useNavigationBuilder.js +1 -0
  11. package/lib/commonjs/useNavigationBuilder.js.map +1 -1
  12. package/lib/commonjs/useNavigationCache.js +9 -0
  13. package/lib/commonjs/useNavigationCache.js.map +1 -1
  14. package/lib/commonjs/useNavigationHelpers.js +18 -3
  15. package/lib/commonjs/useNavigationHelpers.js.map +1 -1
  16. package/lib/module/SceneView.js.map +1 -1
  17. package/lib/module/types.js.map +1 -1
  18. package/lib/module/useChildListeners.js +6 -3
  19. package/lib/module/useChildListeners.js.map +1 -1
  20. package/lib/module/useDescriptors.js.map +1 -1
  21. package/lib/module/useEventEmitter.js +12 -2
  22. package/lib/module/useEventEmitter.js.map +1 -1
  23. package/lib/module/useKeyedChildListeners.js +2 -0
  24. package/lib/module/useKeyedChildListeners.js.map +1 -1
  25. package/lib/module/useNavigationBuilder.js +1 -0
  26. package/lib/module/useNavigationBuilder.js.map +1 -1
  27. package/lib/module/useNavigationCache.js +9 -0
  28. package/lib/module/useNavigationCache.js.map +1 -1
  29. package/lib/module/useNavigationHelpers.js +18 -3
  30. package/lib/module/useNavigationHelpers.js.map +1 -1
  31. package/lib/typescript/src/NavigationBuilderContext.d.ts +1 -1
  32. package/lib/typescript/src/NavigationContext.d.ts +1 -1
  33. package/lib/typescript/src/SceneView.d.ts +1 -1
  34. package/lib/typescript/src/types.d.ts +39 -18
  35. package/lib/typescript/src/useDescriptors.d.ts +8 -6
  36. package/lib/typescript/src/useNavigationBuilder.d.ts +12 -73
  37. package/lib/typescript/src/useNavigationCache.d.ts +1 -1
  38. package/lib/typescript/src/useNavigationHelpers.d.ts +7 -69
  39. package/package.json +7 -8
  40. package/src/SceneView.tsx +7 -1
  41. package/src/types.tsx +51 -15
  42. package/src/useChildListeners.tsx +5 -3
  43. package/src/useDescriptors.tsx +8 -1
  44. package/src/useEventEmitter.tsx +11 -2
  45. package/src/useKeyedChildListeners.tsx +2 -0
  46. package/src/useNavigationBuilder.tsx +1 -0
  47. package/src/useNavigationCache.tsx +17 -1
  48. package/src/useNavigationHelpers.tsx +33 -15
@@ -85,23 +85,8 @@ export default function useNavigationBuilder<State extends NavigationState, Rout
85
85
  goBack(): void;
86
86
  isFocused(): boolean;
87
87
  canGoBack(): boolean;
88
- getParent<T = import("./types").NavigationProp<ParamListBase, string, Readonly<{
89
- key: string;
90
- index: number;
91
- routeNames: string[];
92
- history?: unknown[] | undefined;
93
- routes: (Readonly<{
94
- key: string;
95
- name: string;
96
- path?: string | undefined;
97
- }> & Readonly<{
98
- params?: Readonly<object | undefined>;
99
- }> & {
100
- state?: Readonly<any> | PartialState<Readonly<any>> | undefined;
101
- })[];
102
- type: string;
103
- stale: false;
104
- }>, {}, {}> | undefined>(): T;
88
+ getId(): string | undefined;
89
+ getParent<T = import("./types").NavigationHelpers<ParamListBase, {}> | undefined>(id?: string | undefined): T;
105
90
  getState(): Readonly<{
106
91
  key: string;
107
92
  index: number;
@@ -119,58 +104,10 @@ export default function useNavigationBuilder<State extends NavigationState, Rout
119
104
  type: string;
120
105
  stale: false;
121
106
  }>;
122
- } & PrivateValueStore<ParamListBase, string, {}> & import("./types").EventEmitter<EventMap> & {
107
+ } & PrivateValueStore<[ParamListBase, unknown, unknown]> & import("./types").EventEmitter<EventMap> & {
123
108
  setParams<RouteName_2 extends string>(params: Partial<object | undefined>): void;
124
- } & {
125
- dispatch(action: Readonly<{
126
- type: string;
127
- payload?: object | undefined;
128
- source?: string | undefined;
129
- target?: string | undefined;
130
- }> | ((state: any) => Readonly<{
131
- type: string;
132
- payload?: object | undefined;
133
- source?: string | undefined;
134
- target?: string | undefined;
135
- }>)): void;
136
- navigate<RouteName extends string>(...args: [screen: RouteName] | [screen: RouteName, params: object | undefined]): void;
137
- navigate<RouteName_1 extends string>(options: {
138
- key: string;
139
- params?: object | undefined;
140
- merge?: boolean | undefined;
141
- } | {
142
- name: RouteName_1;
143
- key?: string | undefined;
144
- params: object | undefined;
145
- merge?: boolean | undefined;
146
- }): void;
147
- reset(state: any): void;
148
- goBack(): void;
149
- isFocused(): boolean;
150
- canGoBack(): boolean;
151
- getParent<T = import("./types").NavigationProp<ParamListBase, string, Readonly<{
152
- key: string;
153
- index: number;
154
- routeNames: string[];
155
- history?: unknown[] | undefined;
156
- routes: (Readonly<{
157
- key: string;
158
- name: string;
159
- path?: string | undefined;
160
- }> & Readonly<{
161
- params?: Readonly<object | undefined>;
162
- }> & {
163
- state?: Readonly<any> | PartialState<Readonly<any>> | undefined;
164
- })[];
165
- type: string;
166
- stale: false;
167
- }>, {}, {}> | undefined>(): T;
168
- getState(): any;
169
- } & {
170
- setParams(params: Partial<object | undefined>): void;
171
- setOptions(options: Partial<any>): void;
172
- } & import("./types").EventConsumer<any> & PrivateValueStore<ParamListBase, string, any> & ActionHelpers;
173
- descriptors: Record<string, import("./types").Descriptor<ScreenOptions, {
109
+ } & ActionHelpers;
110
+ descriptors: Record<string, import("./types").Descriptor<ScreenOptions, Omit<{
174
111
  dispatch(action: Readonly<{
175
112
  type: string;
176
113
  payload?: object | undefined;
@@ -197,7 +134,11 @@ export default function useNavigationBuilder<State extends NavigationState, Rout
197
134
  goBack(): void;
198
135
  isFocused(): boolean;
199
136
  canGoBack(): boolean;
200
- getParent<T = import("./types").NavigationProp<ParamListBase, string, Readonly<{
137
+ getId(): string | undefined;
138
+ getParent<T = import("./types").NavigationHelpers<ParamListBase, {}> | undefined>(id?: string | undefined): T;
139
+ getState(): State;
140
+ } & PrivateValueStore<[ParamListBase, unknown, unknown]>, "getParent"> & {
141
+ getParent<T_1 = import("./types").NavigationProp<ParamListBase, string, undefined, Readonly<{
201
142
  key: string;
202
143
  index: number;
203
144
  routeNames: string[];
@@ -213,11 +154,9 @@ export default function useNavigationBuilder<State extends NavigationState, Rout
213
154
  })[];
214
155
  type: string;
215
156
  stale: false;
216
- }>, {}, {}> | undefined>(): T;
217
- getState(): State;
218
- } & PrivateValueStore<ParamListBase, string, {}> & {
157
+ }>, {}, {}> | undefined>(id?: string | undefined): T_1;
219
158
  setParams(params: Partial<object | undefined>): void;
220
159
  setOptions(options: Partial<ScreenOptions>): void;
221
- } & import("./types").EventConsumer<EventMap & EventMapCore<State>> & PrivateValueStore<ParamListBase, string, EventMap> & ActionHelpers, import("./types").RouteProp<ParamListBase, string>>>;
160
+ } & import("./types").EventConsumer<EventMap & EventMapCore<State>> & PrivateValueStore<[ParamListBase, string, EventMap]> & ActionHelpers, import("./types").RouteProp<ParamListBase, string>>>;
222
161
  NavigationContent: (rest: Omit<React.ProviderProps<import("./types").NavigationHelpers<ParamListBase, {}> | undefined>, "value">) => JSX.Element;
223
162
  };
@@ -9,7 +9,7 @@ declare type Options<State extends NavigationState, EventMap extends Record<stri
9
9
  router: Router<State, NavigationAction>;
10
10
  emitter: NavigationEventEmitter<EventMap>;
11
11
  };
12
- declare type NavigationCache<State extends NavigationState, ScreenOptions extends {}, EventMap extends Record<string, any>> = Record<string, NavigationProp<ParamListBase, string, State, ScreenOptions, EventMap>>;
12
+ declare type NavigationCache<State extends NavigationState, ScreenOptions extends {}, EventMap extends Record<string, any>> = Record<string, NavigationProp<ParamListBase, string, string | undefined, State, ScreenOptions, EventMap>>;
13
13
  /**
14
14
  * Hook to cache navigation objects for each screen in the navigator.
15
15
  * It's important to cache them to make sure navigation objects don't change between renders.
@@ -1,7 +1,8 @@
1
1
  import { NavigationAction, NavigationState, ParamListBase, Router } from '@react-navigation/routers';
2
- import { NavigationProp, PrivateValueStore } from './types';
2
+ import { NavigationHelpers, PrivateValueStore } from './types';
3
3
  import type { NavigationEventEmitter } from './useEventEmitter';
4
4
  declare type Options<State extends NavigationState, Action extends NavigationAction> = {
5
+ id: string | undefined;
5
6
  onAction: (action: NavigationAction) => boolean;
6
7
  getState: () => State;
7
8
  emitter: NavigationEventEmitter<any>;
@@ -11,7 +12,7 @@ declare type Options<State extends NavigationState, Action extends NavigationAct
11
12
  * Navigation object with helper methods to be used by a navigator.
12
13
  * This object includes methods for common actions as well as methods the parent screen's navigation object.
13
14
  */
14
- export default function useNavigationHelpers<State extends NavigationState, ActionHelpers extends Record<string, () => void>, Action extends NavigationAction, EventMap extends Record<string, any>>({ onAction, getState, emitter, router }: Options<State, Action>): {
15
+ export default function useNavigationHelpers<State extends NavigationState, ActionHelpers extends Record<string, () => void>, Action extends NavigationAction, EventMap extends Record<string, any>>({ id: navigatorId, onAction, getState, emitter, router, }: Options<State, Action>): {
15
16
  dispatch(action: Readonly<{
16
17
  type: string;
17
18
  payload?: object | undefined;
@@ -86,23 +87,8 @@ export default function useNavigationHelpers<State extends NavigationState, Acti
86
87
  goBack(): void;
87
88
  isFocused(): boolean;
88
89
  canGoBack(): boolean;
89
- getParent<T = NavigationProp<ParamListBase, string, Readonly<{
90
- key: string;
91
- index: number;
92
- routeNames: string[];
93
- history?: unknown[] | undefined;
94
- routes: (Readonly<{
95
- key: string;
96
- name: string;
97
- path?: string | undefined;
98
- }> & Readonly<{
99
- params?: Readonly<object | undefined>;
100
- }> & {
101
- state?: Readonly<any> | import("@react-navigation/routers").PartialState<Readonly<any>> | undefined;
102
- })[];
103
- type: string;
104
- stale: false;
105
- }>, {}, {}> | undefined>(): T;
90
+ getId(): string | undefined;
91
+ getParent<T = NavigationHelpers<ParamListBase, {}> | undefined>(id?: string | undefined): T;
106
92
  getState(): Readonly<{
107
93
  key: string;
108
94
  index: number;
@@ -120,55 +106,7 @@ export default function useNavigationHelpers<State extends NavigationState, Acti
120
106
  type: string;
121
107
  stale: false;
122
108
  }>;
123
- } & PrivateValueStore<ParamListBase, string, {}> & import("./types").EventEmitter<EventMap> & {
109
+ } & PrivateValueStore<[ParamListBase, unknown, unknown]> & import("./types").EventEmitter<EventMap> & {
124
110
  setParams<RouteName_2 extends string>(params: Partial<object | undefined>): void;
125
- } & {
126
- dispatch(action: Readonly<{
127
- type: string;
128
- payload?: object | undefined;
129
- source?: string | undefined;
130
- target?: string | undefined;
131
- }> | ((state: any) => Readonly<{
132
- type: string;
133
- payload?: object | undefined;
134
- source?: string | undefined;
135
- target?: string | undefined;
136
- }>)): void;
137
- navigate<RouteName extends string>(...args: [screen: RouteName] | [screen: RouteName, params: object | undefined]): void;
138
- navigate<RouteName_1 extends string>(options: {
139
- key: string;
140
- params?: object | undefined;
141
- merge?: boolean | undefined;
142
- } | {
143
- name: RouteName_1;
144
- key?: string | undefined;
145
- params: object | undefined;
146
- merge?: boolean | undefined;
147
- }): void;
148
- reset(state: any): void;
149
- goBack(): void;
150
- isFocused(): boolean;
151
- canGoBack(): boolean;
152
- getParent<T = NavigationProp<ParamListBase, string, Readonly<{
153
- key: string;
154
- index: number;
155
- routeNames: string[];
156
- history?: unknown[] | undefined;
157
- routes: (Readonly<{
158
- key: string;
159
- name: string;
160
- path?: string | undefined;
161
- }> & Readonly<{
162
- params?: Readonly<object | undefined>;
163
- }> & {
164
- state?: Readonly<any> | import("@react-navigation/routers").PartialState<Readonly<any>> | undefined;
165
- })[];
166
- type: string;
167
- stale: false;
168
- }>, {}, {}> | undefined>(): T;
169
- getState(): any;
170
- } & {
171
- setParams(params: Partial<object | undefined>): void;
172
- setOptions(options: Partial<any>): void;
173
- } & import("./types").EventConsumer<any> & PrivateValueStore<ParamListBase, string, any> & ActionHelpers;
111
+ } & ActionHelpers;
174
112
  export {};
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@react-navigation/core",
3
3
  "description": "Core utilities for building navigators",
4
- "version": "6.1.1",
4
+ "version": "6.2.2",
5
5
  "keywords": [
6
6
  "react",
7
7
  "react-native",
@@ -35,7 +35,7 @@
35
35
  "clean": "del lib"
36
36
  },
37
37
  "dependencies": {
38
- "@react-navigation/routers": "^6.1.0",
38
+ "@react-navigation/routers": "^6.1.1",
39
39
  "escape-string-regexp": "^4.0.0",
40
40
  "nanoid": "^3.1.23",
41
41
  "query-string": "^7.0.0",
@@ -43,14 +43,14 @@
43
43
  },
44
44
  "devDependencies": {
45
45
  "@testing-library/react-native": "^7.2.0",
46
- "@types/react": "^17.0.9",
46
+ "@types/react": "^17.0.47",
47
47
  "@types/react-is": "^17.0.0",
48
48
  "del-cli": "^3.0.1",
49
49
  "immer": "^9.0.2",
50
- "react": "17.0.1",
50
+ "react": "17.0.2",
51
51
  "react-native-builder-bob": "^0.18.1",
52
- "react-test-renderer": "17.0.1",
53
- "typescript": "^4.3.2"
52
+ "react-test-renderer": "17.0.2",
53
+ "typescript": "^4.7.4"
54
54
  },
55
55
  "peerDependencies": {
56
56
  "react": "*"
@@ -68,6 +68,5 @@
68
68
  }
69
69
  ]
70
70
  ]
71
- },
72
- "gitHead": "4c5805867ce946d99ba17f71b4d6086bb8751262"
71
+ }
73
72
  }
package/src/SceneView.tsx CHANGED
@@ -14,7 +14,13 @@ import useOptionsGetters from './useOptionsGetters';
14
14
 
15
15
  type Props<State extends NavigationState, ScreenOptions extends {}> = {
16
16
  screen: RouteConfigComponent<ParamListBase, string> & { name: string };
17
- navigation: NavigationProp<ParamListBase, string, State, ScreenOptions>;
17
+ navigation: NavigationProp<
18
+ ParamListBase,
19
+ string,
20
+ string | undefined,
21
+ State,
22
+ ScreenOptions
23
+ >;
18
24
  route: Route<string>;
19
25
  routeState: NavigationState | PartialState<NavigationState> | undefined;
20
26
  getState: () => State;
package/src/types.tsx CHANGED
@@ -25,6 +25,10 @@ export type DefaultNavigatorOptions<
25
25
  ScreenOptions extends {},
26
26
  EventMap extends EventMapBase
27
27
  > = DefaultRouterOptions<Keyof<ParamList>> & {
28
+ /**
29
+ * Optional ID for the navigator. Can be used with `navigation.getParent(id)` to refer to a parent.
30
+ */
31
+ id?: string;
28
32
  /**
29
33
  * Children React Elements to extract the route configuration from.
30
34
  * Only `Screen`, `Group` and `React.Fragment` are supported as children.
@@ -153,7 +157,7 @@ export type EventEmitter<EventMap extends EventMapBase> = {
153
157
  >;
154
158
  };
155
159
 
156
- export class PrivateValueStore<A, B, C> {
160
+ export class PrivateValueStore<T extends [any, any, any]> {
157
161
  /**
158
162
  * UGLY HACK! DO NOT USE THE TYPE!!!
159
163
  *
@@ -164,7 +168,7 @@ export class PrivateValueStore<A, B, C> {
164
168
  * Adding private keyword works, but the annotation is stripped away in declaration.
165
169
  * Turns out if we use an empty string, it doesn't show up in intelliSense.
166
170
  */
167
- protected ''?: { a: A; b: B; c: C };
171
+ protected ''?: T;
168
172
  }
169
173
 
170
174
  type NavigationHelpersCommon<
@@ -236,16 +240,26 @@ type NavigationHelpersCommon<
236
240
  canGoBack(): boolean;
237
241
 
238
242
  /**
239
- * Returns the navigation prop from the parent navigator,
243
+ * Returns the name of the navigator specified in the `name` prop.
244
+ * If no name is specified, returns `undefined`.
245
+ */
246
+ getId(): string | undefined;
247
+
248
+ /**
249
+ * Returns the navigation helpers from a parent navigator based on the ID.
250
+ * If an ID is provided, the navigation helper from the parent navigator with matching ID (including current) will be returned.
251
+ * If no ID is provided, the navigation helper from the immediate parent navigator will be returned.
252
+ *
253
+ * @param id Optional ID of a parent navigator.
240
254
  */
241
- getParent<T = NavigationProp<ParamListBase> | undefined>(): T;
255
+ getParent<T = NavigationHelpers<ParamListBase> | undefined>(id?: string): T;
242
256
 
243
257
  /**
244
258
  * Returns the navigator's state.
245
259
  * Note that this method doesn't re-render screen when the result changes. So don't use it in `render`.
246
260
  */
247
261
  getState(): State;
248
- } & PrivateValueStore<ParamList, keyof ParamList, {}>;
262
+ } & PrivateValueStore<[ParamList, unknown, unknown]>;
249
263
 
250
264
  export type NavigationHelpers<
251
265
  ParamList extends ParamListBase,
@@ -291,10 +305,20 @@ export type NavigationContainerProps = {
291
305
  export type NavigationProp<
292
306
  ParamList extends {},
293
307
  RouteName extends keyof ParamList = Keyof<ParamList>,
308
+ NavigatorID extends string | undefined = undefined,
294
309
  State extends NavigationState = NavigationState<ParamList>,
295
310
  ScreenOptions extends {} = {},
296
311
  EventMap extends EventMapBase = {}
297
- > = NavigationHelpersCommon<ParamList, State> & {
312
+ > = Omit<NavigationHelpersCommon<ParamList, State>, 'getParent'> & {
313
+ /**
314
+ * Returns the navigation prop from a parent navigator based on the ID.
315
+ * If an ID is provided, the navigation prop from the parent navigator with matching ID (including current) will be returned.
316
+ * If no ID is provided, the navigation prop from the immediate parent navigator will be returned.
317
+ *
318
+ * @param id Optional ID of a parent navigator.
319
+ */
320
+ getParent<T = NavigationProp<ParamListBase> | undefined>(id?: NavigatorID): T;
321
+
298
322
  /**
299
323
  * Update the param object for the route.
300
324
  * The new params will be shallow merged with the old one.
@@ -311,7 +335,7 @@ export type NavigationProp<
311
335
  */
312
336
  setOptions(options: Partial<ScreenOptions>): void;
313
337
  } & EventConsumer<EventMap & EventMapCore<State>> &
314
- PrivateValueStore<ParamList, RouteName, EventMap>;
338
+ PrivateValueStore<[ParamList, RouteName, EventMap]>;
315
339
 
316
340
  export type RouteProp<
317
341
  ParamList extends ParamListBase,
@@ -319,7 +343,7 @@ export type RouteProp<
319
343
  > = Route<Extract<RouteName, string>, ParamList[RouteName]>;
320
344
 
321
345
  export type CompositeNavigationProp<
322
- A extends NavigationProp<ParamListBase, string, any, any>,
346
+ A extends NavigationProp<ParamListBase, string, any, any, any>,
323
347
  B extends NavigationHelpersCommon<ParamListBase, any>
324
348
  > = Omit<A & B, keyof NavigationProp<any>> &
325
349
  NavigationProp<
@@ -334,30 +358,42 @@ export type CompositeNavigationProp<
334
358
  * Ideally it should work for any of them, but it's not possible to infer that way
335
359
  */
336
360
  A extends NavigationProp<any, infer R> ? R : string,
361
+ /**
362
+ * ID from both navigation objects needs to be combined for `getParent`
363
+ */
364
+ | (A extends NavigationProp<any, any, infer I> ? I : never)
365
+ | (B extends NavigationProp<any, any, infer J> ? J : never),
337
366
  /**
338
367
  * The type of state should refer to the state specified in the first type
339
368
  */
340
- A extends NavigationProp<any, any, infer S> ? S : NavigationState,
369
+ A extends NavigationProp<any, any, any, infer S> ? S : NavigationState,
341
370
  /**
342
371
  * Screen options from both navigation objects needs to be combined
343
372
  * This allows typechecking `setOptions`
344
373
  */
345
- (A extends NavigationProp<any, any, any, infer O> ? O : {}) &
346
- (B extends NavigationProp<any, any, any, infer P> ? P : {}),
374
+ (A extends NavigationProp<any, any, any, any, infer O> ? O : {}) &
375
+ (B extends NavigationProp<any, any, any, any, infer P> ? P : {}),
347
376
  /**
348
377
  * Event consumer config should refer to the config specified in the first type
349
378
  * This allows typechecking `addListener`/`removeListener`
350
379
  */
351
- A extends NavigationProp<any, any, any, any, infer E> ? E : {}
380
+ A extends NavigationProp<any, any, any, any, any, infer E> ? E : {}
352
381
  >;
353
382
 
354
383
  export type CompositeScreenProps<
355
384
  A extends {
356
- navigation: NavigationProp<ParamListBase, string, any, any>;
385
+ navigation: NavigationProp<
386
+ ParamListBase,
387
+ string,
388
+ string | undefined,
389
+ any,
390
+ any,
391
+ any
392
+ >;
357
393
  route: RouteProp<ParamListBase>;
358
394
  },
359
395
  B extends {
360
- navigation: NavigationHelpersCommon<ParamListBase, any>;
396
+ navigation: NavigationHelpersCommon<any, any>;
361
397
  }
362
398
  > = {
363
399
  navigation: CompositeNavigationProp<A['navigation'], B['navigation']>;
@@ -366,7 +402,7 @@ export type CompositeScreenProps<
366
402
 
367
403
  export type Descriptor<
368
404
  ScreenOptions extends {},
369
- Navigation extends NavigationProp<any, any, any, any, any>,
405
+ Navigation extends NavigationProp<any, any, any, any, any, any>,
370
406
  Route extends RouteProp<any, any>
371
407
  > = {
372
408
  /**
@@ -15,14 +15,16 @@ export default function useChildListeners() {
15
15
 
16
16
  const addListener = React.useCallback(
17
17
  <T extends keyof ListenerMap>(type: T, listener: ListenerMap[T]) => {
18
- // @ts-expect-error: listener should be correct type according to `type`
19
18
  listeners[type].push(listener);
20
19
 
20
+ let removed = false;
21
21
  return () => {
22
- // @ts-expect-error: listener should be correct type according to `type`
23
22
  const index = listeners[type].indexOf(listener);
24
23
 
25
- listeners[type].splice(index, 1);
24
+ if (!removed && index > -1) {
25
+ removed = true;
26
+ listeners[type].splice(index, 1);
27
+ }
26
28
  };
27
29
  },
28
30
  [listeners]
@@ -143,7 +143,14 @@ export default function useDescriptors<
143
143
  string,
144
144
  Descriptor<
145
145
  ScreenOptions,
146
- NavigationProp<ParamListBase, string, State, ScreenOptions, EventMap> &
146
+ NavigationProp<
147
+ ParamListBase,
148
+ string,
149
+ string | undefined,
150
+ State,
151
+ ScreenOptions,
152
+ EventMap
153
+ > &
147
154
  ActionHelpers,
148
155
  RouteProp<ParamListBase>
149
156
  >
@@ -35,7 +35,9 @@ export default function useEventEmitter<T extends Record<string, any>>(
35
35
 
36
36
  const index = callbacks.indexOf(callback);
37
37
 
38
- callbacks.splice(index, 1);
38
+ if (index > -1) {
39
+ callbacks.splice(index, 1);
40
+ }
39
41
  };
40
42
 
41
43
  const addListener = (type: string, callback: (data: any) => void) => {
@@ -43,7 +45,14 @@ export default function useEventEmitter<T extends Record<string, any>>(
43
45
  listeners.current[type][target] = listeners.current[type][target] || [];
44
46
  listeners.current[type][target].push(callback);
45
47
 
46
- return () => removeListener(type, callback);
48
+ let removed = false;
49
+ return () => {
50
+ // Prevent removing other listeners when unsubscribing same listener multiple times
51
+ if (!removed) {
52
+ removed = true;
53
+ removeListener(type, callback);
54
+ }
55
+ };
47
56
  };
48
57
 
49
58
  return {
@@ -22,9 +22,11 @@ export default function useKeyedChildListeners() {
22
22
  key: string,
23
23
  listener: KeyedListenerMap[T]
24
24
  ) => {
25
+ // @ts-expect-error: according to ref stated above you can use `key` to index type
25
26
  keyedListeners[type][key] = listener;
26
27
 
27
28
  return () => {
29
+ // @ts-expect-error: according to ref stated above you can use `key` to index type
28
30
  keyedListeners[type][key] = undefined;
29
31
  };
30
32
  },
@@ -645,6 +645,7 @@ export default function useNavigationBuilder<
645
645
  NavigationAction,
646
646
  EventMap
647
647
  >({
648
+ id: options.id,
648
649
  onAction,
649
650
  getState,
650
651
  emitter,
@@ -32,7 +32,14 @@ type NavigationCache<
32
32
  EventMap extends Record<string, any>
33
33
  > = Record<
34
34
  string,
35
- NavigationProp<ParamListBase, string, State, ScreenOptions, EventMap>
35
+ NavigationProp<
36
+ ParamListBase,
37
+ string,
38
+ string | undefined,
39
+ State,
40
+ ScreenOptions,
41
+ EventMap
42
+ >
36
43
  >;
37
44
 
38
45
  /**
@@ -133,6 +140,15 @@ export default function useNavigationCache<
133
140
  // FIXME: too much work to fix the types for now
134
141
  ...(emitter.create(route.key) as any),
135
142
  dispatch: (thunk: Thunk) => withStack(() => dispatch(thunk)),
143
+ getParent: (id?: string) => {
144
+ if (id !== undefined && id === rest.getId()) {
145
+ // If the passed id is the same as the current navigation id,
146
+ // we return the cached navigation object for the relevant route
147
+ return acc[route.key];
148
+ }
149
+
150
+ return rest.getParent(id);
151
+ },
136
152
  setOptions: (options: object) =>
137
153
  setOptions((o) => ({
138
154
  ...o,
@@ -8,7 +8,7 @@ import {
8
8
  import * as React from 'react';
9
9
 
10
10
  import NavigationContext from './NavigationContext';
11
- import { NavigationHelpers, NavigationProp, PrivateValueStore } from './types';
11
+ import { NavigationHelpers, PrivateValueStore } from './types';
12
12
  import UnhandledActionContext from './UnhandledActionContext';
13
13
  import type { NavigationEventEmitter } from './useEventEmitter';
14
14
 
@@ -17,6 +17,7 @@ import type { NavigationEventEmitter } from './useEventEmitter';
17
17
  PrivateValueStore;
18
18
 
19
19
  type Options<State extends NavigationState, Action extends NavigationAction> = {
20
+ id: string | undefined;
20
21
  onAction: (action: NavigationAction) => boolean;
21
22
  getState: () => State;
22
23
  emitter: NavigationEventEmitter<any>;
@@ -32,7 +33,13 @@ export default function useNavigationHelpers<
32
33
  ActionHelpers extends Record<string, () => void>,
33
34
  Action extends NavigationAction,
34
35
  EventMap extends Record<string, any>
35
- >({ onAction, getState, emitter, router }: Options<State, Action>) {
36
+ >({
37
+ id: navigatorId,
38
+ onAction,
39
+ getState,
40
+ emitter,
41
+ router,
42
+ }: Options<State, Action>) {
36
43
  const onUnhandledAction = React.useContext(UnhandledActionContext);
37
44
  const parentNavigationHelpers = React.useContext(NavigationContext);
38
45
 
@@ -52,16 +59,13 @@ export default function useNavigationHelpers<
52
59
  ...CommonActions,
53
60
  };
54
61
 
55
- const helpers = Object.keys(actions).reduce<Record<string, () => void>>(
56
- (acc, name) => {
57
- // @ts-expect-error: name is a valid key, but TypeScript is dumb
58
- acc[name] = (...args: any) => dispatch(actions[name](...args));
59
- return acc;
60
- },
61
- {}
62
- );
62
+ const helpers = Object.keys(actions).reduce((acc, name) => {
63
+ // @ts-expect-error: name is a valid key, but TypeScript is dumb
64
+ acc[name] = (...args: any) => dispatch(actions[name](...args));
65
+ return acc;
66
+ }, {} as ActionHelpers);
63
67
 
64
- return {
68
+ const navigationHelpers = {
65
69
  ...parentNavigationHelpers,
66
70
  ...helpers,
67
71
  dispatch,
@@ -82,12 +86,26 @@ export default function useNavigationHelpers<
82
86
  false
83
87
  );
84
88
  },
85
- getParent: () => parentNavigationHelpers as any,
89
+ getId: () => navigatorId,
90
+ getParent: (id?: string) => {
91
+ if (id !== undefined) {
92
+ let current = navigationHelpers;
93
+
94
+ while (current && id !== current.getId()) {
95
+ current = current.getParent();
96
+ }
97
+
98
+ return current;
99
+ }
100
+
101
+ return parentNavigationHelpers;
102
+ },
86
103
  getState,
87
- } as NavigationHelpers<ParamListBase, EventMap> &
88
- (NavigationProp<ParamListBase, string, any, any, any> | undefined) &
89
- ActionHelpers;
104
+ } as NavigationHelpers<ParamListBase, EventMap> & ActionHelpers;
105
+
106
+ return navigationHelpers;
90
107
  }, [
108
+ navigatorId,
91
109
  emitter.emit,
92
110
  getState,
93
111
  onAction,