@react-navigation/core 7.3.0 → 7.4.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 (48) hide show
  1. package/lib/commonjs/StaticNavigation.js +2 -2
  2. package/lib/commonjs/StaticNavigation.js.map +1 -1
  3. package/lib/commonjs/getActionFromState.js +25 -0
  4. package/lib/commonjs/getActionFromState.js.map +1 -1
  5. package/lib/commonjs/useNavigationBuilder.js +2 -1
  6. package/lib/commonjs/useNavigationBuilder.js.map +1 -1
  7. package/lib/commonjs/useScheduleUpdate.js +2 -1
  8. package/lib/commonjs/useScheduleUpdate.js.map +1 -1
  9. package/lib/module/StaticNavigation.js +2 -2
  10. package/lib/module/StaticNavigation.js.map +1 -1
  11. package/lib/module/getActionFromState.js +25 -0
  12. package/lib/module/getActionFromState.js.map +1 -1
  13. package/lib/module/useNavigationBuilder.js +2 -1
  14. package/lib/module/useNavigationBuilder.js.map +1 -1
  15. package/lib/module/useScheduleUpdate.js +3 -1
  16. package/lib/module/useScheduleUpdate.js.map +1 -1
  17. package/lib/typescript/commonjs/src/StaticNavigation.d.ts.map +1 -1
  18. package/lib/typescript/commonjs/src/getActionFromState.d.ts.map +1 -1
  19. package/lib/typescript/commonjs/src/types.d.ts +9 -1
  20. package/lib/typescript/commonjs/src/types.d.ts.map +1 -1
  21. package/lib/typescript/commonjs/src/useDescriptors.d.ts +2 -0
  22. package/lib/typescript/commonjs/src/useDescriptors.d.ts.map +1 -1
  23. package/lib/typescript/commonjs/src/useNavigationBuilder.d.ts +3 -0
  24. package/lib/typescript/commonjs/src/useNavigationBuilder.d.ts.map +1 -1
  25. package/lib/typescript/commonjs/src/useNavigationCache.d.ts +1 -0
  26. package/lib/typescript/commonjs/src/useNavigationCache.d.ts.map +1 -1
  27. package/lib/typescript/commonjs/src/useNavigationHelpers.d.ts +1 -0
  28. package/lib/typescript/commonjs/src/useNavigationHelpers.d.ts.map +1 -1
  29. package/lib/typescript/commonjs/src/useScheduleUpdate.d.ts.map +1 -1
  30. package/lib/typescript/module/src/StaticNavigation.d.ts.map +1 -1
  31. package/lib/typescript/module/src/getActionFromState.d.ts.map +1 -1
  32. package/lib/typescript/module/src/types.d.ts +9 -1
  33. package/lib/typescript/module/src/types.d.ts.map +1 -1
  34. package/lib/typescript/module/src/useDescriptors.d.ts +2 -0
  35. package/lib/typescript/module/src/useDescriptors.d.ts.map +1 -1
  36. package/lib/typescript/module/src/useNavigationBuilder.d.ts +3 -0
  37. package/lib/typescript/module/src/useNavigationBuilder.d.ts.map +1 -1
  38. package/lib/typescript/module/src/useNavigationCache.d.ts +1 -0
  39. package/lib/typescript/module/src/useNavigationCache.d.ts.map +1 -1
  40. package/lib/typescript/module/src/useNavigationHelpers.d.ts +1 -0
  41. package/lib/typescript/module/src/useNavigationHelpers.d.ts.map +1 -1
  42. package/lib/typescript/module/src/useScheduleUpdate.d.ts.map +1 -1
  43. package/package.json +4 -4
  44. package/src/StaticNavigation.tsx +2 -4
  45. package/src/getActionFromState.tsx +35 -1
  46. package/src/types.tsx +9 -1
  47. package/src/useNavigationBuilder.tsx +1 -0
  48. package/src/useScheduleUpdate.tsx +3 -1
@@ -65,10 +65,35 @@ export function getActionFromState(
65
65
  let config: ConfigItem | undefined = normalizedConfig?.screens?.[route?.name];
66
66
  let params = { ...route.params } as NavigatorScreenParams<ParamListBase>;
67
67
 
68
- const payload = route
68
+ const payload:
69
+ | {
70
+ name: string;
71
+ params: NavigatorScreenParams<ParamListBase>;
72
+ path?: string;
73
+ pop?: boolean;
74
+ }
75
+ | undefined = route
69
76
  ? { name: route.name, path: route.path, params }
70
77
  : undefined;
71
78
 
79
+ // If the screen contains a navigator, pop other screens to navigate to it
80
+ // This avoid pushing multiple instances of navigators onto a stack
81
+ //
82
+ // For example:
83
+ // - RootStack
84
+ // - BottomTabs
85
+ // - SomeScreen
86
+ //
87
+ // In this case, if deep linking to `BottomTabs`, we should pop `SomeScreen`
88
+ // Otherwise, we'll end up with 2 instances of `BottomTabs` in the stack
89
+ //
90
+ // There are 2 ways we can detect if a screen contains a navigator:
91
+ // - The route contains nested state in `route.state`
92
+ // - Nested screens are defined in the config
93
+ if (payload && config?.screens && Object.keys(config.screens).length) {
94
+ payload.pop = true;
95
+ }
96
+
72
97
  while (current) {
73
98
  if (current.routes.length === 0) {
74
99
  return undefined;
@@ -108,6 +133,7 @@ export function getActionFromState(
108
133
 
109
134
  if (route.state) {
110
135
  params.params = { ...route.params };
136
+ params.pop = true;
111
137
  params = params.params as NavigatorScreenParams<ParamListBase>;
112
138
  } else {
113
139
  params.path = route.path;
@@ -116,6 +142,14 @@ export function getActionFromState(
116
142
 
117
143
  current = route.state;
118
144
  config = config?.screens?.[route.name];
145
+
146
+ if (config?.screens && Object.keys(config.screens).length) {
147
+ params.pop = true;
148
+ }
149
+ }
150
+
151
+ if (payload?.params.screen || payload?.params.state) {
152
+ payload.pop = true;
119
153
  }
120
154
 
121
155
  if (!payload) {
package/src/types.tsx CHANGED
@@ -271,7 +271,11 @@ type NavigationHelpersCommon<
271
271
  /**
272
272
  * Navigate to a route in current navigation tree.
273
273
  *
274
- * @param options Object with `name` for the route to navigate to, and a `params` object.
274
+ * @param options.name Name of the route to navigate to.
275
+ * @param [options.params] Params object for the route.
276
+ * @param [options.path] Path to associate the route with (e.g. for deep links).
277
+ * @param [options.merge] Whether to merge the params onto the route.
278
+ * @param [options.pop] Whether to pop routes in a stack to go back to the matching route.
275
279
  */
276
280
  navigate<RouteName extends keyof ParamList>(
277
281
  options: {
@@ -280,6 +284,7 @@ type NavigationHelpersCommon<
280
284
  params: ParamList[Screen];
281
285
  path?: string;
282
286
  merge?: boolean;
287
+ pop?: boolean;
283
288
  };
284
289
  }[RouteName]
285
290
  ): void;
@@ -950,6 +955,7 @@ export type NavigatorScreenParams<ParamList extends {}> =
950
955
  screen?: never;
951
956
  params?: never;
952
957
  initial?: never;
958
+ pop?: never;
953
959
  path?: string;
954
960
  state: PartialState<NavigationState> | NavigationState | undefined;
955
961
  }
@@ -960,6 +966,7 @@ export type NavigatorScreenParams<ParamList extends {}> =
960
966
  params?: ParamList[RouteName];
961
967
  initial?: boolean;
962
968
  path?: string;
969
+ pop?: boolean;
963
970
  state?: never;
964
971
  }
965
972
  : {
@@ -967,6 +974,7 @@ export type NavigatorScreenParams<ParamList extends {}> =
967
974
  params: ParamList[RouteName];
968
975
  initial?: boolean;
969
976
  path?: string;
977
+ pop?: boolean;
970
978
  state?: never;
971
979
  };
972
980
  }[keyof ParamList];
@@ -538,6 +538,7 @@ export function useNavigationBuilder<
538
538
  name: route.params.screen,
539
539
  params: route.params.params,
540
540
  path: route.params.path,
541
+ pop: route.params.pop,
541
542
  });
542
543
  }
543
544
 
@@ -1,6 +1,8 @@
1
1
  import * as React from 'react';
2
2
 
3
3
  import { NavigationBuilderContext } from './NavigationBuilderContext';
4
+ import { useIsomorphicLayoutEffect } from './useIsomorphicLayoutEffect';
5
+
4
6
  /**
5
7
  * When screen config changes, we want to update the navigator in the same update phase.
6
8
  * However, navigation state is in the root component and React won't let us update it from a child.
@@ -17,5 +19,5 @@ export function useScheduleUpdate(callback: () => void) {
17
19
  // However, since we are using sync store, it might be fine
18
20
  scheduleUpdate(callback);
19
21
 
20
- React.useEffect(flushUpdates);
22
+ useIsomorphicLayoutEffect(flushUpdates);
21
23
  }