@react-navigation/core 7.22.1 → 7.24.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 (95) hide show
  1. package/lib/module/BaseNavigationContainer.js +50 -22
  2. package/lib/module/BaseNavigationContainer.js.map +1 -1
  3. package/lib/module/NavigationBuilderContext.js +7 -11
  4. package/lib/module/NavigationBuilderContext.js.map +1 -1
  5. package/lib/module/NavigationIndependentTree.js +41 -10
  6. package/lib/module/NavigationIndependentTree.js.map +1 -1
  7. package/lib/module/NavigationMetaContext.js +3 -3
  8. package/lib/module/NavigationProvider.js +2 -2
  9. package/lib/module/NavigationProvider.js.map +1 -1
  10. package/lib/module/NavigationStateContext.js.map +1 -1
  11. package/lib/module/PreventRemoveProvider.js +3 -4
  12. package/lib/module/PreventRemoveProvider.js.map +1 -1
  13. package/lib/module/SceneView.js +18 -16
  14. package/lib/module/SceneView.js.map +1 -1
  15. package/lib/module/StaticNavigation.js +53 -18
  16. package/lib/module/StaticNavigation.js.map +1 -1
  17. package/lib/module/isRecordEqual.js +1 -1
  18. package/lib/module/isRecordEqual.js.map +1 -1
  19. package/lib/module/useDescriptors.js +40 -15
  20. package/lib/module/useDescriptors.js.map +1 -1
  21. package/lib/module/useFocusedListenersChildrenAdapter.js +2 -2
  22. package/lib/module/useFocusedListenersChildrenAdapter.js.map +1 -1
  23. package/lib/module/useNavigationBuilder.js +3 -2
  24. package/lib/module/useNavigationBuilder.js.map +1 -1
  25. package/lib/module/useNavigationCache.js +14 -26
  26. package/lib/module/useNavigationCache.js.map +1 -1
  27. package/lib/module/useNavigationHelpers.js +17 -7
  28. package/lib/module/useNavigationHelpers.js.map +1 -1
  29. package/lib/module/useNavigationState.js +1 -1
  30. package/lib/module/useNavigationState.js.map +1 -1
  31. package/lib/module/useOnAction.js +2 -2
  32. package/lib/module/useOnAction.js.map +1 -1
  33. package/lib/module/useOnGetState.js +2 -2
  34. package/lib/module/useOnGetState.js.map +1 -1
  35. package/lib/module/useOnPreventRemove.js +2 -2
  36. package/lib/module/useOnPreventRemove.js.map +1 -1
  37. package/lib/module/useOnRouteFocus.js +2 -2
  38. package/lib/module/useOnRouteFocus.js.map +1 -1
  39. package/lib/module/useOptionsGetters.js +4 -8
  40. package/lib/module/useOptionsGetters.js.map +1 -1
  41. package/lib/module/usePreventRemove.js +1 -2
  42. package/lib/module/usePreventRemove.js.map +1 -1
  43. package/lib/module/useRegisterNavigator.js +1 -2
  44. package/lib/module/useRegisterNavigator.js.map +1 -1
  45. package/lib/module/useScheduleUpdate.js +2 -2
  46. package/lib/module/useScheduleUpdate.js.map +1 -1
  47. package/lib/typescript/src/BaseNavigationContainer.d.ts.map +1 -1
  48. package/lib/typescript/src/NavigationBuilderContext.d.ts +11 -7
  49. package/lib/typescript/src/NavigationBuilderContext.d.ts.map +1 -1
  50. package/lib/typescript/src/NavigationIndependentTree.d.ts.map +1 -1
  51. package/lib/typescript/src/NavigationMetaContext.d.ts +3 -3
  52. package/lib/typescript/src/NavigationProvider.d.ts.map +1 -1
  53. package/lib/typescript/src/NavigationStateContext.d.ts +0 -1
  54. package/lib/typescript/src/NavigationStateContext.d.ts.map +1 -1
  55. package/lib/typescript/src/PreventRemoveProvider.d.ts.map +1 -1
  56. package/lib/typescript/src/SceneView.d.ts.map +1 -1
  57. package/lib/typescript/src/StaticNavigation.d.ts.map +1 -1
  58. package/lib/typescript/src/isRecordEqual.d.ts.map +1 -1
  59. package/lib/typescript/src/useDescriptors.d.ts.map +1 -1
  60. package/lib/typescript/src/useNavigationBuilder.d.ts.map +1 -1
  61. package/lib/typescript/src/useNavigationCache.d.ts.map +1 -1
  62. package/lib/typescript/src/useNavigationHelpers.d.ts.map +1 -1
  63. package/lib/typescript/src/useNavigationState.d.ts +5 -0
  64. package/lib/typescript/src/useNavigationState.d.ts.map +1 -1
  65. package/lib/typescript/src/useOnRouteFocus.d.ts.map +1 -1
  66. package/lib/typescript/src/useOptionsGetters.d.ts +2 -0
  67. package/lib/typescript/src/useOptionsGetters.d.ts.map +1 -1
  68. package/lib/typescript/src/usePreventRemove.d.ts.map +1 -1
  69. package/lib/typescript/src/useRegisterNavigator.d.ts.map +1 -1
  70. package/lib/typescript/src/useScheduleUpdate.d.ts.map +1 -1
  71. package/package.json +3 -4
  72. package/src/BaseNavigationContainer.tsx +73 -33
  73. package/src/NavigationBuilderContext.tsx +27 -17
  74. package/src/NavigationIndependentTree.tsx +41 -13
  75. package/src/NavigationMetaContext.tsx +3 -3
  76. package/src/NavigationProvider.tsx +11 -8
  77. package/src/NavigationStateContext.tsx +0 -4
  78. package/src/PreventRemoveProvider.tsx +4 -10
  79. package/src/SceneView.tsx +18 -25
  80. package/src/StaticNavigation.tsx +71 -17
  81. package/src/isRecordEqual.tsx +3 -1
  82. package/src/useDescriptors.tsx +99 -27
  83. package/src/useFocusedListenersChildrenAdapter.tsx +2 -2
  84. package/src/useNavigationBuilder.tsx +3 -4
  85. package/src/useNavigationCache.tsx +13 -30
  86. package/src/useNavigationHelpers.tsx +18 -6
  87. package/src/useNavigationState.tsx +1 -1
  88. package/src/useOnAction.tsx +2 -2
  89. package/src/useOnGetState.tsx +2 -2
  90. package/src/useOnPreventRemove.tsx +2 -2
  91. package/src/useOnRouteFocus.tsx +2 -4
  92. package/src/useOptionsGetters.tsx +9 -6
  93. package/src/usePreventRemove.tsx +1 -2
  94. package/src/useRegisterNavigator.tsx +2 -2
  95. package/src/useScheduleUpdate.tsx +2 -4
@@ -356,6 +356,9 @@ const MemoizedScreen = React.memo(
356
356
 
357
357
  MemoizedScreen.displayName = 'Memo(Screen)';
358
358
 
359
+ const hasPropertyGetter = (object: object, property: PropertyKey) =>
360
+ Object.getOwnPropertyDescriptor(object, property)?.get != null;
361
+
359
362
  const getItemsFromScreens = (
360
363
  Screen: React.ComponentType<any>,
361
364
  screens: StaticConfigScreens<any, any, any, any, any>
@@ -366,18 +369,57 @@ const getItemsFromScreens = (
366
369
  let useIf: (() => boolean) | undefined;
367
370
 
368
371
  let isNavigator = false;
372
+ let getComponent: (() => React.ComponentType<any>) | undefined;
369
373
 
370
374
  if ('screen' in item) {
371
- const { screen, if: _if, ...rest } = item;
372
-
373
- useIf = _if;
374
- props = rest;
375
+ if (hasPropertyGetter(item, 'screen')) {
376
+ const rest: Record<string, unknown> = {};
377
+
378
+ // Iterate over the keys to avoid triggering the getter for 'screen'
379
+ for (const key of Object.keys(item)) {
380
+ if (key === 'screen' || key === 'if') {
381
+ continue;
382
+ }
383
+
384
+ rest[key] = Reflect.get(item, key);
385
+ }
386
+
387
+ useIf = item.if;
388
+ props = rest;
389
+
390
+ getComponent = () => {
391
+ const screen = item.screen;
392
+
393
+ if (isValidElementType(screen)) {
394
+ return screen;
395
+ }
396
+
397
+ if (
398
+ screen != null &&
399
+ typeof screen === 'object' &&
400
+ 'config' in screen
401
+ ) {
402
+ throw new Error(
403
+ `Got a navigator object for the screen '${name}' instead of a component. A navigator cannot be specified when using a getter.`
404
+ );
405
+ }
375
406
 
376
- if (isValidElementType(screen)) {
377
- component = screen;
378
- } else if ('config' in screen) {
379
- isNavigator = true;
380
- component = screen.getComponent();
407
+ throw new Error(
408
+ `Got an invalid screen component for the screen '${name}'. This can happen if you passed 'undefined'. You likely forgot to export your component from the file it's defined in, or mixed up default import and named import when importing.`
409
+ );
410
+ };
411
+ } else {
412
+ const { screen, if: _if, ...rest } = item;
413
+
414
+ useIf = _if;
415
+ props = rest;
416
+
417
+ if (isValidElementType(screen)) {
418
+ component = screen;
419
+ } else if ('config' in screen) {
420
+ isNavigator = true;
421
+ component = screen.getComponent();
422
+ }
381
423
  }
382
424
  } else if (isValidElementType(item)) {
383
425
  component = item;
@@ -386,18 +428,29 @@ const getItemsFromScreens = (
386
428
  component = item.getComponent();
387
429
  }
388
430
 
389
- if (component == null) {
431
+ let children: (() => React.JSX.Element) | undefined;
432
+
433
+ if (getComponent != null) {
434
+ let element: React.JSX.Element | undefined;
435
+
436
+ children = () =>
437
+ (element ??= <MemoizedScreen component={getComponent()} />);
438
+ } else if (component != null) {
439
+ const element = isNavigator ? (
440
+ React.createElement(component, {})
441
+ ) : (
442
+ <MemoizedScreen component={component} />
443
+ );
444
+
445
+ children = () => element;
446
+ }
447
+
448
+ if (children == null) {
390
449
  throw new Error(
391
450
  `Couldn't find a 'screen' property for the screen '${name}'. This can happen if you passed 'undefined'. You likely forgot to export your component from the file it's defined in, or mixed up default import and named import when importing.`
392
451
  );
393
452
  }
394
453
 
395
- const element = isNavigator ? (
396
- React.createElement(component, {})
397
- ) : (
398
- <MemoizedScreen component={component} />
399
- );
400
-
401
454
  return () => {
402
455
  const shouldRender = useIf == null || useIf();
403
456
 
@@ -407,7 +460,7 @@ const getItemsFromScreens = (
407
460
 
408
461
  return (
409
462
  <Screen key={name} name={name} {...props}>
410
- {() => element}
463
+ {children}
411
464
  </Screen>
412
465
  );
413
466
  };
@@ -732,6 +785,7 @@ export function createPathConfigForStaticNavigation(
732
785
  !hasExplicitScreens &&
733
786
  !hasDisabledLinking &&
734
787
  'screen' in item &&
788
+ !hasPropertyGetter(item, 'screen') &&
735
789
  'config' in item.screen &&
736
790
  (item.screen.config.screens || item.screen.config.groups)
737
791
  ) {
@@ -16,5 +16,7 @@ export function isRecordEqual(
16
16
  return false;
17
17
  }
18
18
 
19
- return aKeys.every((key) => Object.is(a[key], b[key]));
19
+ return aKeys.every(
20
+ (key) => Object.hasOwn(b, key) && Object.is(a[key], b[key])
21
+ );
20
22
  }
@@ -7,10 +7,13 @@ import type {
7
7
  } from '@react-navigation/routers';
8
8
  import * as React from 'react';
9
9
 
10
+ import { isArrayEqual } from './isArrayEqual';
11
+ import { isRecordEqual } from './isRecordEqual';
10
12
  import {
11
13
  type AddKeyedListener,
12
14
  type AddListener,
13
15
  NavigationBuilderContext,
16
+ useNavigationBuilderContext,
14
17
  } from './NavigationBuilderContext';
15
18
  import { NavigationProvider } from './NavigationProvider';
16
19
  import { SceneView } from './SceneView';
@@ -84,6 +87,31 @@ type Options<
84
87
  emitter: NavigationEventEmitter<EventMap>;
85
88
  };
86
89
 
90
+ type DescriptorCache<
91
+ State extends NavigationState,
92
+ ScreenOptions extends {},
93
+ EventMap extends EventMapBase,
94
+ ActionHelpers extends Record<string, () => void>,
95
+ > = Record<
96
+ string,
97
+ {
98
+ descriptor: Descriptor<
99
+ ScreenOptions,
100
+ NavigationProp<
101
+ ParamListBase,
102
+ string,
103
+ string | undefined,
104
+ State,
105
+ ScreenOptions,
106
+ EventMap
107
+ > &
108
+ ActionHelpers,
109
+ RouteProp<ParamListBase>
110
+ >;
111
+ deps: unknown[];
112
+ }
113
+ >;
114
+
87
115
  /**
88
116
  * Hook to create descriptor objects for the child routes.
89
117
  *
@@ -122,9 +150,9 @@ export function useDescriptors<
122
150
  onOptionsChange,
123
151
  scheduleUpdate,
124
152
  flushUpdates,
125
- stackRef,
153
+ withStackTrace,
126
154
  getIsStateEmitted,
127
- } = React.useContext(NavigationBuilderContext);
155
+ } = useNavigationBuilderContext();
128
156
 
129
157
  const context = React.useMemo(
130
158
  () => ({
@@ -139,7 +167,7 @@ export function useDescriptors<
139
167
  getIsStateEmitted,
140
168
  scheduleUpdate,
141
169
  flushUpdates,
142
- stackRef,
170
+ withStackTrace,
143
171
  }),
144
172
  [
145
173
  navigation,
@@ -153,7 +181,7 @@ export function useDescriptors<
153
181
  getIsStateEmitted,
154
182
  scheduleUpdate,
155
183
  flushUpdates,
156
- stackRef,
184
+ withStackTrace,
157
185
  ]
158
186
  );
159
187
 
@@ -173,6 +201,10 @@ export function useDescriptors<
173
201
 
174
202
  const routes = useRouteCache(state.routes);
175
203
 
204
+ const cache = React.useMemo<{
205
+ current: DescriptorCache<State, ScreenOptions, EventMap, ActionHelpers>;
206
+ }>(() => ({ current: {} }), []);
207
+
176
208
  const getOptions = (
177
209
  route: RouteProp<ParamListBase, string>,
178
210
  navigation: NavigationProp<
@@ -223,7 +255,8 @@ export function useDescriptors<
223
255
  EventMap
224
256
  >,
225
257
  customOptions: ScreenOptions,
226
- routeState: NavigationState | PartialState<NavigationState> | undefined
258
+ routeState: NavigationState | PartialState<NavigationState> | undefined,
259
+ layout: ScreenLayout<ScreenOptions> | undefined
227
260
  ) => {
228
261
  const config = screens[route.name];
229
262
  const screen = config.props;
@@ -238,14 +271,6 @@ export function useDescriptors<
238
271
  return o;
239
272
  });
240
273
 
241
- const layout =
242
- // The `layout` prop passed to `Screen` elements,
243
- screen.layout ??
244
- // The `screenLayout` props passed to `Group` elements
245
- config.layout ??
246
- // The default `screenLayout` passed to the navigator
247
- screenLayout;
248
-
249
274
  let element = (
250
275
  <SceneView
251
276
  navigation={navigation}
@@ -279,31 +304,67 @@ export function useDescriptors<
279
304
  );
280
305
  };
281
306
 
307
+ const next: DescriptorCache<State, ScreenOptions, EventMap, ActionHelpers> =
308
+ {};
309
+
282
310
  const descriptors = routes.reduce<
283
311
  Record<
284
312
  string,
285
- Descriptor<
313
+ DescriptorCache<
314
+ State,
286
315
  ScreenOptions,
287
- NavigationProp<
288
- ParamListBase,
289
- string,
290
- string | undefined,
291
- State,
292
- ScreenOptions,
293
- EventMap
294
- > &
295
- ActionHelpers,
296
- RouteProp<ParamListBase>
297
- >
316
+ EventMap,
317
+ ActionHelpers
318
+ >[string]['descriptor']
298
319
  >
299
320
  >((acc, route, i) => {
300
321
  const navigation = navigations[route.key];
301
322
  const customOptions = getOptions(route, navigation, options[route.key]);
323
+ const routeState = state.routes[i].state;
324
+ const config = screens[route.name];
325
+ const screen = config.props;
326
+
327
+ const layout =
328
+ // The `layout` prop passed to `Screen` elements,
329
+ screen.layout ??
330
+ // The `screenLayout` props passed to `Group` elements
331
+ config.layout ??
332
+ // The default `screenLayout` passed to the navigator
333
+ screenLayout;
334
+
335
+ const deps = [
336
+ route,
337
+ navigation,
338
+ routeState,
339
+ screen.component,
340
+ screen.getComponent,
341
+ screen.children,
342
+ layout,
343
+ context,
344
+ theme,
345
+ getState,
346
+ setState,
347
+ ];
348
+
349
+ const previous = cache.current[route.key];
350
+
351
+ if (
352
+ previous &&
353
+ isArrayEqual(previous.deps, deps) &&
354
+ isRecordEqual(previous.descriptor.options, customOptions)
355
+ ) {
356
+ next[route.key] = previous;
357
+ acc[route.key] = previous.descriptor;
358
+
359
+ return acc;
360
+ }
361
+
302
362
  const element = render(
303
363
  route,
304
364
  navigation,
305
365
  customOptions,
306
- state.routes[i].state
366
+ routeState,
367
+ layout
307
368
  );
308
369
 
309
370
  acc[route.key] = {
@@ -316,9 +377,18 @@ export function useDescriptors<
316
377
  options: customOptions,
317
378
  };
318
379
 
380
+ next[route.key] = {
381
+ descriptor: acc[route.key],
382
+ deps,
383
+ };
384
+
319
385
  return acc;
320
386
  }, {});
321
387
 
388
+ React.useInsertionEffect(() => {
389
+ cache.current = next;
390
+ });
391
+
322
392
  /**
323
393
  * Create a descriptor object for a route.
324
394
  *
@@ -337,7 +407,9 @@ export function useDescriptors<
337
407
 
338
408
  const navigation = base;
339
409
  const customOptions = getOptions(route, navigation, {});
340
- const element = render(route, navigation, customOptions, undefined);
410
+ const config = screens[route.name];
411
+ const layout = config.props.layout ?? config.layout ?? screenLayout;
412
+ const element = render(route, navigation, customOptions, undefined, layout);
341
413
 
342
414
  return {
343
415
  route,
@@ -4,7 +4,7 @@ import * as React from 'react';
4
4
  import {
5
5
  type FocusedNavigationCallback,
6
6
  type FocusedNavigationListener,
7
- NavigationBuilderContext,
7
+ useNavigationBuilderContext,
8
8
  } from './NavigationBuilderContext';
9
9
  import type { NavigationHelpers } from './types';
10
10
 
@@ -20,7 +20,7 @@ export function useFocusedListenersChildrenAdapter({
20
20
  navigation,
21
21
  focusedListeners,
22
22
  }: Options) {
23
- const { addListener } = React.useContext(NavigationBuilderContext);
23
+ const { addListener } = useNavigationBuilderContext();
24
24
 
25
25
  const listener = React.useCallback(
26
26
  (callback: FocusedNavigationCallback<any>) => {
@@ -19,8 +19,9 @@ import { ConsumedParamsContext } from './ConsumedParamsContext';
19
19
  import { deepFreeze } from './deepFreeze';
20
20
  import { Group } from './Group';
21
21
  import { isArrayEqual } from './isArrayEqual';
22
- import { NavigationBuilderContext } from './NavigationBuilderContext';
22
+ import { useNavigationBuilderContext } from './NavigationBuilderContext';
23
23
  import { NavigationHelpersContext } from './NavigationHelpersContext';
24
+ // eslint-disable-next-line import-x/no-deprecated
24
25
  import { NavigationMetaContext } from './NavigationMetaContext';
25
26
  import { NavigationRouteContext } from './NavigationProvider';
26
27
  import { NavigationStateContext } from './NavigationStateContext';
@@ -462,9 +463,7 @@ export function useNavigationBuilder<
462
463
  getIsInitial,
463
464
  } = React.useContext(NavigationStateContext);
464
465
 
465
- const { onEmitEvent, getIsStateEmitted } = React.useContext(
466
- NavigationBuilderContext
467
- );
466
+ const { onEmitEvent, getIsStateEmitted } = useNavigationBuilderContext();
468
467
 
469
468
  const stateCleanupRef = React.useRef<boolean>(false);
470
469
  const lastStateRef = React.useRef<State | PartialState<State> | undefined>(
@@ -7,7 +7,7 @@ import {
7
7
  } from '@react-navigation/routers';
8
8
  import * as React from 'react';
9
9
 
10
- import { NavigationBuilderContext } from './NavigationBuilderContext';
10
+ import { useNavigationBuilderContext } from './NavigationBuilderContext';
11
11
  import type { NavigationHelpers, NavigationProp } from './types';
12
12
  import type { NavigationEventEmitter } from './useEventEmitter';
13
13
 
@@ -66,7 +66,7 @@ export function useNavigationCache<
66
66
  router,
67
67
  emitter,
68
68
  }: Options<State, ScreenOptions, EventMap>) {
69
- const { stackRef } = React.useContext(NavigationBuilderContext);
69
+ const { withStackTrace } = useNavigationBuilderContext();
70
70
 
71
71
  const base = React.useMemo((): NavigationItem<
72
72
  State,
@@ -148,33 +148,14 @@ export function useNavigationCache<
148
148
  acc[route.key] = previous;
149
149
  } else {
150
150
  const dispatch = (thunk: Thunk) => {
151
- const action = typeof thunk === 'function' ? thunk(getState()) : thunk;
151
+ withStackTrace(dispatch, () => {
152
+ const action =
153
+ typeof thunk === 'function' ? thunk(getState()) : thunk;
152
154
 
153
- if (action != null) {
154
- navigation.dispatch({ source: route.key, ...action });
155
- }
156
- };
157
-
158
- const withStack = (callback: () => void) => {
159
- let isStackSet = false;
160
-
161
- try {
162
- if (
163
- process.env.NODE_ENV !== 'production' &&
164
- stackRef &&
165
- !stackRef.current
166
- ) {
167
- // Capture the stack trace for devtools
168
- stackRef.current = new Error().stack;
169
- isStackSet = true;
155
+ if (action != null) {
156
+ navigation.dispatch({ source: route.key, ...action });
170
157
  }
171
-
172
- callback();
173
- } finally {
174
- if (isStackSet && stackRef) {
175
- stackRef.current = undefined;
176
- }
177
- }
158
+ });
178
159
  };
179
160
 
180
161
  const actions = {
@@ -184,12 +165,14 @@ export function useNavigationCache<
184
165
 
185
166
  const helpers = Object.keys(actions).reduce<Record<string, () => void>>(
186
167
  (acc, name) => {
187
- acc[name] = (...args: any) =>
188
- withStack(() =>
168
+ const helper = (...args: any) =>
169
+ withStackTrace(helper, () =>
189
170
  // @ts-expect-error: name is a valid key, but TypeScript is dumb
190
171
  dispatch(actions[name](...args))
191
172
  );
192
173
 
174
+ acc[name] = helper;
175
+
193
176
  return acc;
194
177
  },
195
178
  {}
@@ -200,7 +183,7 @@ export function useNavigationCache<
200
183
  ...helpers,
201
184
  // FIXME: too much work to fix the types for now
202
185
  ...(emitter.create(route.key) as any),
203
- dispatch: (thunk: Thunk) => withStack(() => dispatch(thunk)),
186
+ dispatch,
204
187
  getParent: (id?: string) => {
205
188
  if (id !== undefined && id === base.getId()) {
206
189
  // If the passed id is the same as the current navigation id,
@@ -7,6 +7,7 @@ import {
7
7
  } from '@react-navigation/routers';
8
8
  import * as React from 'react';
9
9
 
10
+ import { useNavigationBuilderContext } from './NavigationBuilderContext';
10
11
  import { NavigationContext } from './NavigationContext';
11
12
  import { type NavigationHelpers, PrivateValueStore } from './types';
12
13
  import type { NavigationEventEmitter } from './useEventEmitter';
@@ -44,6 +45,7 @@ export function useNavigationHelpers<
44
45
  router,
45
46
  }: Options<State, Action>) {
46
47
  const parentNavigationHelpers = React.useContext(NavigationContext);
48
+ const { withStackTrace } = useNavigationBuilderContext();
47
49
 
48
50
  // In some cases (e.g. route names change), internal state might have changed
49
51
  // But it hasn't been committed yet, so hasn't propagated to any listeners
@@ -64,13 +66,15 @@ export function useNavigationHelpers<
64
66
 
65
67
  return React.useMemo(() => {
66
68
  const dispatch = (op: Action | ((state: State) => Action)) => {
67
- const action = typeof op === 'function' ? op(getState()) : op;
69
+ withStackTrace(dispatch, () => {
70
+ const action = typeof op === 'function' ? op(getState()) : op;
68
71
 
69
- const handled = onAction(action);
72
+ const handled = onAction(action);
70
73
 
71
- if (!handled) {
72
- onUnhandledAction?.(action);
73
- }
74
+ if (!handled) {
75
+ onUnhandledAction?.(action);
76
+ }
77
+ });
74
78
  };
75
79
 
76
80
  const actions = {
@@ -79,8 +83,15 @@ export function useNavigationHelpers<
79
83
  };
80
84
 
81
85
  const helpers = Object.keys(actions).reduce((acc, name) => {
86
+ const helper = (...args: any) =>
87
+ withStackTrace(helper, () =>
88
+ // @ts-expect-error: name is a valid key, but TypeScript is dumb
89
+ dispatch(actions[name](...args))
90
+ );
91
+
82
92
  // @ts-expect-error: name is a valid key, but TypeScript is dumb
83
- acc[name] = (...args: any) => dispatch(actions[name](...args));
93
+ acc[name] = helper;
94
+
84
95
  return acc;
85
96
  }, {} as ActionHelpers);
86
97
 
@@ -143,5 +154,6 @@ export function useNavigationHelpers<
143
154
  onUnhandledAction,
144
155
  navigatorId,
145
156
  stateRef,
157
+ withStackTrace,
146
158
  ]);
147
159
  }
@@ -108,6 +108,6 @@ export function NavigationStateListenerProvider({
108
108
  );
109
109
  }
110
110
 
111
- const NavigationStateListenerContext = React.createContext<
111
+ export const NavigationStateListenerContext = React.createContext<
112
112
  NavigationStateListener | undefined
113
113
  >(undefined);
@@ -11,7 +11,7 @@ import { DeprecatedNavigationInChildContext } from './DeprecatedNavigationInChil
11
11
  import {
12
12
  type ChildActionListener,
13
13
  type ChildBeforeRemoveListener,
14
- NavigationBuilderContext,
14
+ useNavigationBuilderContext,
15
15
  } from './NavigationBuilderContext';
16
16
  import type { EventMapCore } from './types';
17
17
  import type { NavigationEventEmitter } from './useEventEmitter';
@@ -53,7 +53,7 @@ export function useOnAction<State extends NavigationState>({
53
53
  addListener: addListenerParent,
54
54
  onDispatchAction,
55
55
  flushUpdates,
56
- } = React.useContext(NavigationBuilderContext);
56
+ } = useNavigationBuilderContext();
57
57
  const navigationInChildEnabled = React.useContext(
58
58
  DeprecatedNavigationInChildContext
59
59
  );
@@ -4,7 +4,7 @@ import * as React from 'react';
4
4
  import { isArrayEqual } from './isArrayEqual';
5
5
  import {
6
6
  type GetStateListener,
7
- NavigationBuilderContext,
7
+ useNavigationBuilderContext,
8
8
  } from './NavigationBuilderContext';
9
9
  import { NavigationRouteContext } from './NavigationProvider';
10
10
 
@@ -14,7 +14,7 @@ type Options = {
14
14
  };
15
15
 
16
16
  export function useOnGetState({ getState, getStateListeners }: Options) {
17
- const { addKeyedListener } = React.useContext(NavigationBuilderContext);
17
+ const { addKeyedListener } = useNavigationBuilderContext();
18
18
  const route = React.useContext(NavigationRouteContext);
19
19
  const key = route ? route.key : 'root';
20
20
 
@@ -9,7 +9,7 @@ import * as React from 'react';
9
9
 
10
10
  import {
11
11
  type ChildBeforeRemoveListener,
12
- NavigationBuilderContext,
12
+ useNavigationBuilderContext,
13
13
  } from './NavigationBuilderContext';
14
14
  import { NavigationRouteContext } from './NavigationProvider';
15
15
  import type { EventMapCore } from './types';
@@ -107,7 +107,7 @@ export function useOnPreventRemove({
107
107
  emitter,
108
108
  beforeRemoveListeners,
109
109
  }: Options) {
110
- const { addKeyedListener } = React.useContext(NavigationBuilderContext);
110
+ const { addKeyedListener } = useNavigationBuilderContext();
111
111
  const route = React.useContext(NavigationRouteContext);
112
112
  const routeKey = route?.key;
113
113
 
@@ -5,7 +5,7 @@ import type {
5
5
  } from '@react-navigation/routers';
6
6
  import * as React from 'react';
7
7
 
8
- import { NavigationBuilderContext } from './NavigationBuilderContext';
8
+ import { useNavigationBuilderContext } from './NavigationBuilderContext';
9
9
 
10
10
  type Options<State extends NavigationState, Action extends NavigationAction> = {
11
11
  router: Router<State, Action>;
@@ -23,9 +23,7 @@ export function useOnRouteFocus<
23
23
  State extends NavigationState,
24
24
  Action extends NavigationAction,
25
25
  >({ router, getState, key: sourceRouteKey, setState }: Options<State, Action>) {
26
- const { onRouteFocus: onRouteFocusParent } = React.useContext(
27
- NavigationBuilderContext
28
- );
26
+ const { onRouteFocus: onRouteFocusParent } = useNavigationBuilderContext();
29
27
 
30
28
  return React.useCallback(
31
29
  (key: string) => {
@@ -2,9 +2,12 @@ import type { ParamListBase } from '@react-navigation/routers';
2
2
  import * as React from 'react';
3
3
 
4
4
  import { NavigationBuilderContext } from './NavigationBuilderContext';
5
- import { NavigationStateContext } from './NavigationStateContext';
6
5
  import type { NavigationProp } from './types';
7
6
 
7
+ export const OptionsGetterContext = React.createContext<
8
+ ((key: string, getter: () => object | undefined | null) => void) | undefined
9
+ >(undefined);
10
+
8
11
  type Options = {
9
12
  key?: string;
10
13
  navigation?: NavigationProp<ParamListBase>;
@@ -17,17 +20,17 @@ export function useOptionsGetters({ key, options, navigation }: Options) {
17
20
  Record<string, () => object | undefined | null>
18
21
  >({});
19
22
 
20
- const { onOptionsChange } = React.useContext(NavigationBuilderContext);
21
- const { addOptionsGetter: parentAddOptionsGetter } = React.useContext(
22
- NavigationStateContext
23
- );
23
+ const onOptionsChange = React.useContext(
24
+ NavigationBuilderContext
25
+ )?.onOptionsChange;
26
+ const parentAddOptionsGetter = React.useContext(OptionsGetterContext);
24
27
 
25
28
  const optionsChangeListener = React.useCallback(() => {
26
29
  const isFocused = navigation?.isFocused() ?? true;
27
30
  const hasChildren = Object.keys(optionsGettersFromChildRef.current).length;
28
31
 
29
32
  if (isFocused && !hasChildren) {
30
- onOptionsChange(optionsRef.current ?? {});
33
+ onOptionsChange?.(optionsRef.current ?? {});
31
34
  }
32
35
  }, [navigation, onOptionsChange]);
33
36
 
@@ -1,5 +1,4 @@
1
1
  import type { NavigationAction } from '@react-navigation/routers';
2
- import { nanoid } from 'nanoid/non-secure';
3
2
  import * as React from 'react';
4
3
  import useLatestCallback from 'use-latest-callback';
5
4
 
@@ -18,7 +17,7 @@ export function usePreventRemove(
18
17
  preventRemove: boolean,
19
18
  callback: (options: { data: { action: NavigationAction } }) => void
20
19
  ) {
21
- const [id] = React.useState(() => nanoid());
20
+ const id = React.useId();
22
21
 
23
22
  const navigation = useNavigation();
24
23
  const { key: routeKey } = useRoute();