@react-navigation/core 6.1.0 → 6.2.1

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 (133) hide show
  1. package/lib/commonjs/BaseNavigationContainer.js +13 -9
  2. package/lib/commonjs/BaseNavigationContainer.js.map +1 -1
  3. package/lib/commonjs/CurrentRenderContext.js.map +1 -1
  4. package/lib/commonjs/EnsureSingleNavigator.js +5 -4
  5. package/lib/commonjs/EnsureSingleNavigator.js.map +1 -1
  6. package/lib/commonjs/NavigationContainerRefContext.js.map +1 -1
  7. package/lib/commonjs/NavigationContext.js.map +1 -1
  8. package/lib/commonjs/NavigationHelpersContext.js.map +1 -1
  9. package/lib/commonjs/NavigationRouteContext.js.map +1 -1
  10. package/lib/commonjs/SceneView.js +11 -10
  11. package/lib/commonjs/SceneView.js.map +1 -1
  12. package/lib/commonjs/UnhandledActionContext.js.map +1 -1
  13. package/lib/commonjs/createNavigationContainerRef.js +11 -6
  14. package/lib/commonjs/createNavigationContainerRef.js.map +1 -1
  15. package/lib/commonjs/fromEntries.js +3 -1
  16. package/lib/commonjs/fromEntries.js.map +1 -1
  17. package/lib/commonjs/getActionFromState.js +2 -1
  18. package/lib/commonjs/getActionFromState.js.map +1 -1
  19. package/lib/commonjs/getPathFromState.js +13 -3
  20. package/lib/commonjs/getPathFromState.js.map +1 -1
  21. package/lib/commonjs/getStateFromPath.js +12 -2
  22. package/lib/commonjs/getStateFromPath.js.map +1 -1
  23. package/lib/commonjs/index.js +24 -24
  24. package/lib/commonjs/types.js.map +1 -1
  25. package/lib/commonjs/useChildListeners.js.map +1 -1
  26. package/lib/commonjs/useCurrentRender.js +6 -5
  27. package/lib/commonjs/useCurrentRender.js.map +1 -1
  28. package/lib/commonjs/useDescriptors.js +16 -15
  29. package/lib/commonjs/useDescriptors.js.map +1 -1
  30. package/lib/commonjs/useEventEmitter.js +7 -6
  31. package/lib/commonjs/useEventEmitter.js.map +1 -1
  32. package/lib/commonjs/useFocusEvents.js +5 -4
  33. package/lib/commonjs/useFocusEvents.js.map +1 -1
  34. package/lib/commonjs/useFocusedListenersChildrenAdapter.js +5 -4
  35. package/lib/commonjs/useFocusedListenersChildrenAdapter.js.map +1 -1
  36. package/lib/commonjs/useKeyedChildListeners.js.map +1 -1
  37. package/lib/commonjs/useNavigationBuilder.js +10 -5
  38. package/lib/commonjs/useNavigationBuilder.js.map +1 -1
  39. package/lib/commonjs/useNavigationCache.js +26 -10
  40. package/lib/commonjs/useNavigationCache.js.map +1 -1
  41. package/lib/commonjs/useNavigationHelpers.js +28 -10
  42. package/lib/commonjs/useNavigationHelpers.js.map +1 -1
  43. package/lib/commonjs/useOnAction.js +13 -11
  44. package/lib/commonjs/useOnAction.js.map +1 -1
  45. package/lib/commonjs/useOnGetState.js +5 -4
  46. package/lib/commonjs/useOnGetState.js.map +1 -1
  47. package/lib/commonjs/useOnPreventRemove.js +6 -5
  48. package/lib/commonjs/useOnPreventRemove.js.map +1 -1
  49. package/lib/commonjs/useOnRouteFocus.js +7 -6
  50. package/lib/commonjs/useOnRouteFocus.js.map +1 -1
  51. package/lib/commonjs/useOptionsGetters.js +6 -5
  52. package/lib/commonjs/useOptionsGetters.js.map +1 -1
  53. package/lib/commonjs/useRouteCache.js +1 -1
  54. package/lib/commonjs/useScheduleUpdate.js +1 -1
  55. package/lib/commonjs/validatePathConfig.js +5 -2
  56. package/lib/commonjs/validatePathConfig.js.map +1 -1
  57. package/lib/module/BaseNavigationContainer.js +13 -9
  58. package/lib/module/BaseNavigationContainer.js.map +1 -1
  59. package/lib/module/CurrentRenderContext.js.map +1 -1
  60. package/lib/module/EnsureSingleNavigator.js +4 -3
  61. package/lib/module/EnsureSingleNavigator.js.map +1 -1
  62. package/lib/module/NavigationContainerRefContext.js.map +1 -1
  63. package/lib/module/NavigationContext.js.map +1 -1
  64. package/lib/module/NavigationHelpersContext.js.map +1 -1
  65. package/lib/module/NavigationRouteContext.js.map +1 -1
  66. package/lib/module/SceneView.js +11 -10
  67. package/lib/module/SceneView.js.map +1 -1
  68. package/lib/module/UnhandledActionContext.js.map +1 -1
  69. package/lib/module/createNavigationContainerRef.js +10 -5
  70. package/lib/module/createNavigationContainerRef.js.map +1 -1
  71. package/lib/module/fromEntries.js +3 -1
  72. package/lib/module/fromEntries.js.map +1 -1
  73. package/lib/module/getActionFromState.js +2 -1
  74. package/lib/module/getActionFromState.js.map +1 -1
  75. package/lib/module/getPathFromState.js +13 -3
  76. package/lib/module/getPathFromState.js.map +1 -1
  77. package/lib/module/getStateFromPath.js +12 -2
  78. package/lib/module/getStateFromPath.js.map +1 -1
  79. package/lib/module/types.js.map +1 -1
  80. package/lib/module/useChildListeners.js.map +1 -1
  81. package/lib/module/useCurrentRender.js +6 -5
  82. package/lib/module/useCurrentRender.js.map +1 -1
  83. package/lib/module/useDescriptors.js +16 -15
  84. package/lib/module/useDescriptors.js.map +1 -1
  85. package/lib/module/useEventEmitter.js +7 -6
  86. package/lib/module/useEventEmitter.js.map +1 -1
  87. package/lib/module/useFocusEvents.js +5 -4
  88. package/lib/module/useFocusEvents.js.map +1 -1
  89. package/lib/module/useFocusedListenersChildrenAdapter.js +5 -4
  90. package/lib/module/useFocusedListenersChildrenAdapter.js.map +1 -1
  91. package/lib/module/useKeyedChildListeners.js.map +1 -1
  92. package/lib/module/useNavigationBuilder.js +10 -5
  93. package/lib/module/useNavigationBuilder.js.map +1 -1
  94. package/lib/module/useNavigationCache.js +26 -10
  95. package/lib/module/useNavigationCache.js.map +1 -1
  96. package/lib/module/useNavigationHelpers.js +28 -10
  97. package/lib/module/useNavigationHelpers.js.map +1 -1
  98. package/lib/module/useOnAction.js +13 -11
  99. package/lib/module/useOnAction.js.map +1 -1
  100. package/lib/module/useOnGetState.js +5 -4
  101. package/lib/module/useOnGetState.js.map +1 -1
  102. package/lib/module/useOnPreventRemove.js +6 -5
  103. package/lib/module/useOnPreventRemove.js.map +1 -1
  104. package/lib/module/useOnRouteFocus.js +7 -6
  105. package/lib/module/useOnRouteFocus.js.map +1 -1
  106. package/lib/module/useOptionsGetters.js +6 -5
  107. package/lib/module/useOptionsGetters.js.map +1 -1
  108. package/lib/module/validatePathConfig.js +5 -2
  109. package/lib/module/validatePathConfig.js.map +1 -1
  110. package/lib/typescript/src/NavigationContext.d.ts +1 -1
  111. package/lib/typescript/src/SceneView.d.ts +1 -1
  112. package/lib/typescript/src/types.d.ts +39 -18
  113. package/lib/typescript/src/useDescriptors.d.ts +8 -6
  114. package/lib/typescript/src/useNavigationBuilder.d.ts +12 -73
  115. package/lib/typescript/src/useNavigationCache.d.ts +1 -1
  116. package/lib/typescript/src/useNavigationHelpers.d.ts +7 -69
  117. package/package.json +4 -4
  118. package/src/CurrentRenderContext.tsx +3 -2
  119. package/src/EnsureSingleNavigator.tsx +7 -8
  120. package/src/NavigationContainerRefContext.tsx +3 -4
  121. package/src/NavigationContext.tsx +3 -2
  122. package/src/NavigationHelpersContext.tsx +3 -2
  123. package/src/NavigationRouteContext.tsx +3 -2
  124. package/src/SceneView.tsx +7 -1
  125. package/src/UnhandledActionContext.tsx +3 -4
  126. package/src/createNavigationContainerRef.tsx +3 -1
  127. package/src/types.tsx +57 -21
  128. package/src/useChildListeners.tsx +3 -5
  129. package/src/useDescriptors.tsx +8 -1
  130. package/src/useKeyedChildListeners.tsx +6 -8
  131. package/src/useNavigationBuilder.tsx +19 -9
  132. package/src/useNavigationCache.tsx +17 -1
  133. package/src/useNavigationHelpers.tsx +33 -15
@@ -199,13 +199,19 @@ const getRouteConfigsFromChildren = <
199
199
  );
200
200
  }
201
201
 
202
- if (typeof component === 'function' && component.name === 'component') {
203
- // Inline anonymous functions passed in the `component` prop will have the name of the prop
204
- // It's relatively safe to assume that it's not a component since it should also have PascalCase name
205
- // We won't catch all scenarios here, but this should catch a good chunk of incorrect use.
206
- console.warn(
207
- `Looks like you're passing an inline function for 'component' prop for the screen '${name}' (e.g. component={() => <SomeComponent />}). Passing an inline function will cause the component state to be lost on re-render and cause perf issues since it's re-created every render. You can pass the function as children to 'Screen' instead to achieve the desired behaviour.`
208
- );
202
+ if (typeof component === 'function') {
203
+ if (component.name === 'component') {
204
+ // Inline anonymous functions passed in the `component` prop will have the name of the prop
205
+ // It's relatively safe to assume that it's not a component since it should also have PascalCase name
206
+ // We won't catch all scenarios here, but this should catch a good chunk of incorrect use.
207
+ console.warn(
208
+ `Looks like you're passing an inline function for 'component' prop for the screen '${name}' (e.g. component={() => <SomeComponent />}). Passing an inline function will cause the component state to be lost on re-render and cause perf issues since it's re-created every render. You can pass the function as children to 'Screen' instead to achieve the desired behaviour.`
209
+ );
210
+ } else if (/^[a-z]/.test(component.name)) {
211
+ console.warn(
212
+ `Got a component with the name '${component.name}' for the screen '${name}'. React Components must start with an uppercase letter. If you're passing a regular function and not a component, pass it as children to 'Screen' instead. Otherwise capitalize your component's name.`
213
+ );
214
+ }
209
215
  }
210
216
  } else {
211
217
  throw new Error(
@@ -260,8 +266,11 @@ export default function useNavigationBuilder<
260
266
  })
261
267
  );
262
268
 
263
- const routeConfigs =
264
- getRouteConfigsFromChildren<State, ScreenOptions, EventMap>(children);
269
+ const routeConfigs = getRouteConfigsFromChildren<
270
+ State,
271
+ ScreenOptions,
272
+ EventMap
273
+ >(children);
265
274
 
266
275
  const screens = routeConfigs.reduce<
267
276
  Record<string, ScreenConfigWithParent<State, ScreenOptions, EventMap>>
@@ -636,6 +645,7 @@ export default function useNavigationBuilder<
636
645
  NavigationAction,
637
646
  EventMap
638
647
  >({
648
+ id: options.id,
639
649
  onAction,
640
650
  getState,
641
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,