@react-navigation/core 6.1.1 → 6.2.0

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 (32) hide show
  1. package/lib/commonjs/SceneView.js.map +1 -1
  2. package/lib/commonjs/types.js.map +1 -1
  3. package/lib/commonjs/useDescriptors.js.map +1 -1
  4. package/lib/commonjs/useNavigationBuilder.js +1 -0
  5. package/lib/commonjs/useNavigationBuilder.js.map +1 -1
  6. package/lib/commonjs/useNavigationCache.js +9 -0
  7. package/lib/commonjs/useNavigationCache.js.map +1 -1
  8. package/lib/commonjs/useNavigationHelpers.js +22 -3
  9. package/lib/commonjs/useNavigationHelpers.js.map +1 -1
  10. package/lib/module/SceneView.js.map +1 -1
  11. package/lib/module/types.js.map +1 -1
  12. package/lib/module/useDescriptors.js.map +1 -1
  13. package/lib/module/useNavigationBuilder.js +1 -0
  14. package/lib/module/useNavigationBuilder.js.map +1 -1
  15. package/lib/module/useNavigationCache.js +9 -0
  16. package/lib/module/useNavigationCache.js.map +1 -1
  17. package/lib/module/useNavigationHelpers.js +22 -3
  18. package/lib/module/useNavigationHelpers.js.map +1 -1
  19. package/lib/typescript/src/NavigationContext.d.ts +1 -1
  20. package/lib/typescript/src/SceneView.d.ts +1 -1
  21. package/lib/typescript/src/types.d.ts +39 -18
  22. package/lib/typescript/src/useDescriptors.d.ts +8 -6
  23. package/lib/typescript/src/useNavigationBuilder.d.ts +12 -73
  24. package/lib/typescript/src/useNavigationCache.d.ts +1 -1
  25. package/lib/typescript/src/useNavigationHelpers.d.ts +7 -69
  26. package/package.json +2 -2
  27. package/src/SceneView.tsx +7 -1
  28. package/src/types.tsx +51 -15
  29. package/src/useDescriptors.tsx +8 -1
  30. package/src/useNavigationBuilder.tsx +1 -0
  31. package/src/useNavigationCache.tsx +17 -1
  32. package/src/useNavigationHelpers.tsx +39 -15
@@ -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,32 @@ 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
+ if (current == null) {
99
+ throw new Error(
100
+ `Couldn't find a parent navigator with the ID "${id}". Is this navigator nested under another navigator with this ID?`
101
+ );
102
+ }
103
+
104
+ return current;
105
+ }
106
+
107
+ return parentNavigationHelpers;
108
+ },
86
109
  getState,
87
- } as NavigationHelpers<ParamListBase, EventMap> &
88
- (NavigationProp<ParamListBase, string, any, any, any> | undefined) &
89
- ActionHelpers;
110
+ } as NavigationHelpers<ParamListBase, EventMap> & ActionHelpers;
111
+
112
+ return navigationHelpers;
90
113
  }, [
114
+ navigatorId,
91
115
  emitter.emit,
92
116
  getState,
93
117
  onAction,