@react-navigation/core 8.0.0-alpha.25 → 8.0.0-alpha.27

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 (85) hide show
  1. package/lib/module/BaseNavigationContainer.js +12 -6
  2. package/lib/module/BaseNavigationContainer.js.map +1 -1
  3. package/lib/module/ConsumedParamsContext.js.map +1 -1
  4. package/lib/module/DataLoading.js +29 -4
  5. package/lib/module/DataLoading.js.map +1 -1
  6. package/lib/module/NavigationIndependentTree.js +8 -4
  7. package/lib/module/NavigationIndependentTree.js.map +1 -1
  8. package/lib/module/SceneView.js +16 -39
  9. package/lib/module/SceneView.js.map +1 -1
  10. package/lib/module/StaticNavigation.js +4 -3
  11. package/lib/module/StaticNavigation.js.map +1 -1
  12. package/lib/module/findFocusedRoute.js +1 -1
  13. package/lib/module/findFocusedRoute.js.map +1 -1
  14. package/lib/module/getActionFromState.js +1 -2
  15. package/lib/module/getActionFromState.js.map +1 -1
  16. package/lib/module/getPathFromState.js +41 -26
  17. package/lib/module/getPathFromState.js.map +1 -1
  18. package/lib/module/getPatternParts.js +15 -2
  19. package/lib/module/getPatternParts.js.map +1 -1
  20. package/lib/module/getStateFromPath.js +45 -19
  21. package/lib/module/getStateFromPath.js.map +1 -1
  22. package/lib/module/index.js +1 -0
  23. package/lib/module/index.js.map +1 -1
  24. package/lib/module/types.js.map +1 -1
  25. package/lib/module/useFocusEvents.js +6 -2
  26. package/lib/module/useFocusEvents.js.map +1 -1
  27. package/lib/module/useLazyValue.js.map +1 -1
  28. package/lib/module/useNavigationBuilder.js +8 -6
  29. package/lib/module/useNavigationBuilder.js.map +1 -1
  30. package/lib/module/useOnAction.js +6 -16
  31. package/lib/module/useOnAction.js.map +1 -1
  32. package/lib/module/useOnPreventRemove.js +1 -3
  33. package/lib/module/useOnPreventRemove.js.map +1 -1
  34. package/lib/module/useRouteCache.js +1 -3
  35. package/lib/module/useRouteCache.js.map +1 -1
  36. package/lib/module/useSyncState.js +3 -2
  37. package/lib/module/useSyncState.js.map +1 -1
  38. package/lib/typescript/src/BaseNavigationContainer.d.ts.map +1 -1
  39. package/lib/typescript/src/ConsumedParamsContext.d.ts +1 -6
  40. package/lib/typescript/src/ConsumedParamsContext.d.ts.map +1 -1
  41. package/lib/typescript/src/DataLoading.d.ts +1 -0
  42. package/lib/typescript/src/DataLoading.d.ts.map +1 -1
  43. package/lib/typescript/src/NavigationIndependentTree.d.ts.map +1 -1
  44. package/lib/typescript/src/SceneView.d.ts.map +1 -1
  45. package/lib/typescript/src/StaticNavigation.d.ts +39 -30
  46. package/lib/typescript/src/StaticNavigation.d.ts.map +1 -1
  47. package/lib/typescript/src/getPathFromState.d.ts.map +1 -1
  48. package/lib/typescript/src/getPatternParts.d.ts.map +1 -1
  49. package/lib/typescript/src/getStateFromPath.d.ts.map +1 -1
  50. package/lib/typescript/src/index.d.ts +2 -1
  51. package/lib/typescript/src/index.d.ts.map +1 -1
  52. package/lib/typescript/src/types.d.ts +10 -7
  53. package/lib/typescript/src/types.d.ts.map +1 -1
  54. package/lib/typescript/src/useFocusEvents.d.ts.map +1 -1
  55. package/lib/typescript/src/useLazyValue.d.ts.map +1 -1
  56. package/lib/typescript/src/useNavigationBuilder.d.ts.map +1 -1
  57. package/lib/typescript/src/useOnAction.d.ts.map +1 -1
  58. package/lib/typescript/src/useOnPreventRemove.d.ts.map +1 -1
  59. package/lib/typescript/src/useRouteCache.d.ts.map +1 -1
  60. package/lib/typescript/src/useSyncState.d.ts.map +1 -1
  61. package/lib/typescript/src/utilities.d.ts +10 -0
  62. package/lib/typescript/src/utilities.d.ts.map +1 -1
  63. package/package.json +5 -5
  64. package/src/BaseNavigationContainer.tsx +13 -7
  65. package/src/ConsumedParamsContext.tsx +1 -6
  66. package/src/DataLoading.tsx +68 -4
  67. package/src/NavigationIndependentTree.tsx +8 -5
  68. package/src/SceneView.tsx +18 -52
  69. package/src/StaticNavigation.tsx +88 -64
  70. package/src/findFocusedRoute.tsx +1 -1
  71. package/src/getActionFromState.tsx +7 -3
  72. package/src/getPathFromState.tsx +59 -32
  73. package/src/getPatternParts.tsx +15 -2
  74. package/src/getStateFromPath.tsx +77 -17
  75. package/src/index.tsx +5 -0
  76. package/src/types.tsx +14 -7
  77. package/src/useDescriptors.tsx +1 -1
  78. package/src/useFocusEvents.tsx +7 -5
  79. package/src/useLazyValue.tsx +2 -2
  80. package/src/useNavigationBuilder.tsx +12 -7
  81. package/src/useOnAction.tsx +11 -40
  82. package/src/useOnPreventRemove.tsx +4 -3
  83. package/src/useRouteCache.tsx +1 -4
  84. package/src/useSyncState.tsx +3 -2
  85. package/src/utilities.tsx +19 -0
@@ -1,16 +1,14 @@
1
1
  import type {
2
2
  NavigationAction,
3
- NavigationRoute,
4
3
  NavigationState,
5
- ParamListBase,
6
- PartialRoute,
7
4
  PartialState,
8
5
  Router,
9
6
  RouterConfigOptions,
10
7
  } from '@react-navigation/routers';
11
8
  import * as React from 'react';
12
9
 
13
- import { getLoaderForState } from './DataLoading';
10
+ import { ConsumedParamsContext } from './ConsumedParamsContext';
11
+ import { getLoaderForStateChange } from './DataLoading';
14
12
  import {
15
13
  type ChildActionListener,
16
14
  type ChildBeforeRemoveListener,
@@ -32,40 +30,6 @@ type Options<State extends NavigationState> = {
32
30
  emitter: NavigationEventEmitter<EventMapCore<any>>;
33
31
  };
34
32
 
35
- type MaybePartialRoute =
36
- | NavigationRoute<ParamListBase, string>
37
- | PartialRoute<NavigationRoute<ParamListBase, string>>;
38
-
39
- const didFocusedRouteChange = (
40
- currentState: NavigationState | PartialState<NavigationState>,
41
- nextState: NavigationState | PartialState<NavigationState>
42
- ) => {
43
- let currentFocusedRoute: MaybePartialRoute | undefined =
44
- currentState.routes[currentState.index ?? 0];
45
-
46
- let nextFocusedRoute: MaybePartialRoute | undefined =
47
- nextState.routes[nextState.index ?? 0];
48
-
49
- let changed = currentFocusedRoute?.key !== nextFocusedRoute?.key;
50
-
51
- while (
52
- !changed &&
53
- currentFocusedRoute?.state != null &&
54
- nextFocusedRoute?.state != null &&
55
- currentFocusedRoute.state !== nextFocusedRoute.state
56
- ) {
57
- currentFocusedRoute =
58
- currentFocusedRoute.state.routes[currentFocusedRoute.state.index ?? 0];
59
-
60
- nextFocusedRoute =
61
- nextFocusedRoute.state.routes[nextFocusedRoute.state.index ?? 0];
62
-
63
- changed = currentFocusedRoute?.key !== nextFocusedRoute?.key;
64
- }
65
-
66
- return changed;
67
- };
68
-
69
33
  /**
70
34
  * Hook to handle actions for a navigator, including state updates and bubbling.
71
35
  *
@@ -93,6 +57,7 @@ export function useOnAction<State extends NavigationState>({
93
57
  } = React.use(NavigationBuilderContext);
94
58
 
95
59
  const tree = React.use(StaticTreeContext);
60
+ const consumedParams = React.use(ConsumedParamsContext);
96
61
 
97
62
  const routerConfigOptionsRef =
98
63
  React.useRef<RouterConfigOptions>(routerConfigOptions);
@@ -146,8 +111,13 @@ export function useOnAction<State extends NavigationState>({
146
111
 
147
112
  onDispatchAction(action, false);
148
113
 
149
- if (tree && didFocusedRouteChange(state, result)) {
150
- const loader = getLoaderForState(tree, result);
114
+ if (tree) {
115
+ const loader = getLoaderForStateChange(
116
+ tree,
117
+ result,
118
+ state,
119
+ consumedParams
120
+ );
151
121
 
152
122
  loader?.();
153
123
  }
@@ -194,6 +164,7 @@ export function useOnAction<State extends NavigationState>({
194
164
  [
195
165
  actionListeners,
196
166
  beforeRemoveListeners,
167
+ consumedParams,
197
168
  emitter,
198
169
  getState,
199
170
  key,
@@ -39,9 +39,10 @@ export const shouldPreventRemove = (
39
39
  .filter(([, route]) => route.key != null)
40
40
  );
41
41
 
42
- const visitedRouteKeys: Set<string> =
43
- // @ts-expect-error: add this property to mark that we've already emitted this action
44
- action[VISITED_ROUTE_KEYS] ?? new Set<string>();
42
+ const visitedRouteKeys =
43
+ VISITED_ROUTE_KEYS in action && action[VISITED_ROUTE_KEYS] instanceof Set
44
+ ? action[VISITED_ROUTE_KEYS]
45
+ : new Set<string>();
45
46
 
46
47
  const beforeRemoveAction = {
47
48
  ...action,
@@ -41,10 +41,7 @@ export function useRouteCache<State extends NavigationState>(
41
41
  // FIXME: since the state is updated with mutation, the route object cannot be frozen
42
42
  // As a workaround, loop through the object and make the properties readonly
43
43
  // Only needed once per proxy - skip if we're reusing a previously-frozen one
44
- for (const key in proxy) {
45
- // @ts-expect-error: this is fine since we are looping through the object
46
- const value = proxy[key];
47
-
44
+ for (const [key, value] of Object.entries(proxy)) {
48
45
  Object.defineProperty(proxy, key, {
49
46
  enumerable: true,
50
47
  configurable: true,
@@ -26,9 +26,10 @@ const createStore = <T,>(getInitialState: () => T) => {
26
26
 
27
27
  const setState = (newState: T) => {
28
28
  state = deepFreeze(newState);
29
- didUpdate = true;
30
29
 
31
- if (!isBatching) {
30
+ if (isBatching) {
31
+ didUpdate = true;
32
+ } else {
32
33
  listeners.forEach((listener) => listener());
33
34
  }
34
35
  };
package/src/utilities.tsx CHANGED
@@ -26,6 +26,25 @@ export type NoExcessObject<Provided, Allowed> = Record<
26
26
  never
27
27
  >;
28
28
 
29
+ /**
30
+ * Check an object captured as a generic type against an allowed type,
31
+ * e.g. an object returned from a callback isn't checked for excess properties.
32
+ */
33
+ export type NoExcessCallbackReturn<
34
+ Provided extends {},
35
+ Allowed extends {},
36
+ > = Allowed & Provided & NoExcessObject<Provided, Allowed>;
37
+
38
+ /**
39
+ * Apply excess property checking to a value,
40
+ * or to the return value if the value is a function.
41
+ */
42
+ export type NoExcessValue<Provided, Allowed extends {}> = Provided extends (
43
+ ...args: never[]
44
+ ) => infer Return
45
+ ? (...args: never[]) => Allowed & NoExcessObject<Return, Allowed>
46
+ : NoExcessObject<Provided, Allowed>;
47
+
29
48
  /**
30
49
  * We get a union type when using keyof, but we want an intersection instead.
31
50
  * https://stackoverflow.com/a/50375286/1665026