@react-navigation/core 7.0.5 → 7.1.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 (35) hide show
  1. package/lib/commonjs/useNavigationBuilder.js +2 -4
  2. package/lib/commonjs/useNavigationBuilder.js.map +1 -1
  3. package/lib/commonjs/useNavigationCache.js +1 -1
  4. package/lib/commonjs/useNavigationCache.js.map +1 -1
  5. package/lib/commonjs/useNavigationHelpers.js +14 -3
  6. package/lib/commonjs/useNavigationHelpers.js.map +1 -1
  7. package/lib/module/useNavigationBuilder.js +2 -4
  8. package/lib/module/useNavigationBuilder.js.map +1 -1
  9. package/lib/module/useNavigationCache.js +1 -1
  10. package/lib/module/useNavigationCache.js.map +1 -1
  11. package/lib/module/useNavigationHelpers.js +14 -3
  12. package/lib/module/useNavigationHelpers.js.map +1 -1
  13. package/lib/typescript/commonjs/src/types.d.ts +9 -5
  14. package/lib/typescript/commonjs/src/types.d.ts.map +1 -1
  15. package/lib/typescript/commonjs/src/useDescriptors.d.ts +2 -2
  16. package/lib/typescript/commonjs/src/useNavigationBuilder.d.ts +3 -3
  17. package/lib/typescript/commonjs/src/useNavigationBuilder.d.ts.map +1 -1
  18. package/lib/typescript/commonjs/src/useNavigationCache.d.ts +1 -1
  19. package/lib/typescript/commonjs/src/useNavigationHelpers.d.ts +4 -2
  20. package/lib/typescript/commonjs/src/useNavigationHelpers.d.ts.map +1 -1
  21. package/lib/typescript/commonjs/tsconfig.build.tsbuildinfo +1 -1
  22. package/lib/typescript/module/src/types.d.ts +9 -5
  23. package/lib/typescript/module/src/types.d.ts.map +1 -1
  24. package/lib/typescript/module/src/useDescriptors.d.ts +2 -2
  25. package/lib/typescript/module/src/useNavigationBuilder.d.ts +3 -3
  26. package/lib/typescript/module/src/useNavigationBuilder.d.ts.map +1 -1
  27. package/lib/typescript/module/src/useNavigationCache.d.ts +1 -1
  28. package/lib/typescript/module/src/useNavigationHelpers.d.ts +4 -2
  29. package/lib/typescript/module/src/useNavigationHelpers.d.ts.map +1 -1
  30. package/lib/typescript/module/tsconfig.build.tsbuildinfo +1 -1
  31. package/package.json +3 -3
  32. package/src/types.tsx +16 -5
  33. package/src/useNavigationBuilder.tsx +1 -4
  34. package/src/useNavigationCache.tsx +1 -1
  35. package/src/useNavigationHelpers.tsx +18 -4
package/src/types.tsx CHANGED
@@ -248,19 +248,30 @@ type NavigationHelpersCommon<
248
248
  ): void;
249
249
 
250
250
  /**
251
- * Navigate to a route in current navigation tree.
251
+ * Navigate to a screen in the current or parent navigator.
252
+ * If we're already on the screen, update the params instead.
252
253
  *
253
254
  * @param name Name of the route to navigate to.
254
255
  * @param [params] Params object for the route.
256
+ * @param [merge] Whether to merge the params onto the route.
255
257
  */
256
258
  navigate<RouteName extends keyof ParamList>(
257
- ...args: ScreenParamsPair<ParamList, RouteName>
259
+ ...args: {
260
+ [Screen in keyof ParamList]: undefined extends ParamList[Screen]
261
+ ?
262
+ | [screen: Screen]
263
+ | [screen: Screen, params: ParamList[Screen]]
264
+ | [screen: Screen, params: ParamList[Screen], merge: boolean]
265
+ :
266
+ | [screen: Screen, params: ParamList[Screen]]
267
+ | [screen: Screen, params: ParamList[Screen], merge: boolean];
268
+ }[RouteName]
258
269
  ): void;
259
270
 
260
271
  /**
261
272
  * Navigate to a route in current navigation tree.
262
273
  *
263
- * @param route Object with `name` for the route to navigate to, and a `params` object.
274
+ * @param options Object with `name` for the route to navigate to, and a `params` object.
264
275
  */
265
276
  navigate<RouteName extends keyof ParamList>(
266
277
  options: {
@@ -278,7 +289,7 @@ type NavigationHelpersCommon<
278
289
  *
279
290
  * @deprecated Use `navigate` instead.
280
291
  *
281
- * @param name Route name of the route.
292
+ * @param name Name of the route to navigate to.
282
293
  * @param [params] Params object for the route.
283
294
  */
284
295
  navigateDeprecated<RouteName extends keyof ParamList>(
@@ -290,7 +301,7 @@ type NavigationHelpersCommon<
290
301
  *
291
302
  * @deprecated Use `navigate` instead.
292
303
  *
293
- * @param route Object with `name` for the route to navigate to, and a `params` object.
304
+ * @param options Object with `name` for the route to navigate to, and a `params` object.
294
305
  */
295
306
  navigateDeprecated<RouteName extends keyof ParamList>(
296
307
  options: {
@@ -612,10 +612,6 @@ export function useNavigationBuilder<
612
612
  });
613
613
 
614
614
  const getState = useLatestCallback((): State => {
615
- if (stateRef.current != null) {
616
- return stateRef.current;
617
- }
618
-
619
615
  const currentState = getCurrentState();
620
616
 
621
617
  return deepFreeze(
@@ -721,6 +717,7 @@ export function useNavigationBuilder<
721
717
  getState,
722
718
  emitter,
723
719
  router,
720
+ stateRef,
724
721
  });
725
722
 
726
723
  useFocusedListenersChildrenAdapter({
@@ -217,7 +217,7 @@ export function useNavigationCache<
217
217
  }));
218
218
  },
219
219
  isFocused: () => {
220
- const state = getState();
220
+ const state = base.getState();
221
221
 
222
222
  if (state.routes[state.index].key !== route.key) {
223
223
  return false;
@@ -21,6 +21,7 @@ type Options<State extends NavigationState, Action extends NavigationAction> = {
21
21
  getState: () => State;
22
22
  emitter: NavigationEventEmitter<any>;
23
23
  router: Router<State, Action>;
24
+ stateRef: React.RefObject<State | null>;
24
25
  };
25
26
 
26
27
  /**
@@ -38,6 +39,7 @@ export function useNavigationHelpers<
38
39
  getState,
39
40
  emitter,
40
41
  router,
42
+ stateRef,
41
43
  }: Options<State, Action>) {
42
44
  const onUnhandledAction = React.useContext(UnhandledActionContext);
43
45
  const parentNavigationHelpers = React.useContext(NavigationContext);
@@ -99,17 +101,29 @@ export function useNavigationHelpers<
99
101
 
100
102
  return parentNavigationHelpers;
101
103
  },
102
- getState,
104
+ getState: (): State => {
105
+ // FIXME: Workaround for when the state is read during render
106
+ // By this time, we haven't committed the new state yet
107
+ // Without this `useSyncExternalStore` will keep reading the old state
108
+ // This may result in `useNavigationState` or `useIsFocused` returning wrong values
109
+ // Apart from `useSyncExternalStore`, `getState` should never be called during render
110
+ if (stateRef.current != null) {
111
+ return stateRef.current;
112
+ }
113
+
114
+ return getState();
115
+ },
103
116
  } as NavigationHelpers<ParamListBase, EventMap> & ActionHelpers;
104
117
 
105
118
  return navigationHelpers;
106
119
  }, [
107
- navigatorId,
120
+ router,
121
+ parentNavigationHelpers,
108
122
  emitter.emit,
109
123
  getState,
110
124
  onAction,
111
125
  onUnhandledAction,
112
- parentNavigationHelpers,
113
- router,
126
+ navigatorId,
127
+ stateRef,
114
128
  ]);
115
129
  }