@react-navigation/core 7.23.0 → 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 (76) hide show
  1. package/lib/module/BaseNavigationContainer.js +48 -21
  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/NavigationProvider.js +2 -2
  8. package/lib/module/NavigationProvider.js.map +1 -1
  9. package/lib/module/NavigationStateContext.js.map +1 -1
  10. package/lib/module/SceneView.js +18 -16
  11. package/lib/module/SceneView.js.map +1 -1
  12. package/lib/module/StaticNavigation.js +53 -18
  13. package/lib/module/StaticNavigation.js.map +1 -1
  14. package/lib/module/useDescriptors.js +5 -5
  15. package/lib/module/useDescriptors.js.map +1 -1
  16. package/lib/module/useFocusedListenersChildrenAdapter.js +2 -2
  17. package/lib/module/useFocusedListenersChildrenAdapter.js.map +1 -1
  18. package/lib/module/useNavigationBuilder.js +2 -2
  19. package/lib/module/useNavigationBuilder.js.map +1 -1
  20. package/lib/module/useNavigationCache.js +14 -26
  21. package/lib/module/useNavigationCache.js.map +1 -1
  22. package/lib/module/useNavigationHelpers.js +17 -7
  23. package/lib/module/useNavigationHelpers.js.map +1 -1
  24. package/lib/module/useNavigationState.js +1 -1
  25. package/lib/module/useNavigationState.js.map +1 -1
  26. package/lib/module/useOnAction.js +2 -2
  27. package/lib/module/useOnAction.js.map +1 -1
  28. package/lib/module/useOnGetState.js +2 -2
  29. package/lib/module/useOnGetState.js.map +1 -1
  30. package/lib/module/useOnPreventRemove.js +2 -2
  31. package/lib/module/useOnPreventRemove.js.map +1 -1
  32. package/lib/module/useOnRouteFocus.js +2 -2
  33. package/lib/module/useOnRouteFocus.js.map +1 -1
  34. package/lib/module/useOptionsGetters.js +4 -8
  35. package/lib/module/useOptionsGetters.js.map +1 -1
  36. package/lib/module/useScheduleUpdate.js +2 -2
  37. package/lib/module/useScheduleUpdate.js.map +1 -1
  38. package/lib/typescript/src/BaseNavigationContainer.d.ts.map +1 -1
  39. package/lib/typescript/src/NavigationBuilderContext.d.ts +11 -7
  40. package/lib/typescript/src/NavigationBuilderContext.d.ts.map +1 -1
  41. package/lib/typescript/src/NavigationIndependentTree.d.ts.map +1 -1
  42. package/lib/typescript/src/NavigationProvider.d.ts.map +1 -1
  43. package/lib/typescript/src/NavigationStateContext.d.ts +0 -1
  44. package/lib/typescript/src/NavigationStateContext.d.ts.map +1 -1
  45. package/lib/typescript/src/SceneView.d.ts.map +1 -1
  46. package/lib/typescript/src/StaticNavigation.d.ts.map +1 -1
  47. package/lib/typescript/src/useDescriptors.d.ts.map +1 -1
  48. package/lib/typescript/src/useNavigationBuilder.d.ts.map +1 -1
  49. package/lib/typescript/src/useNavigationCache.d.ts.map +1 -1
  50. package/lib/typescript/src/useNavigationHelpers.d.ts.map +1 -1
  51. package/lib/typescript/src/useNavigationState.d.ts +5 -0
  52. package/lib/typescript/src/useNavigationState.d.ts.map +1 -1
  53. package/lib/typescript/src/useOnRouteFocus.d.ts.map +1 -1
  54. package/lib/typescript/src/useOptionsGetters.d.ts +2 -0
  55. package/lib/typescript/src/useOptionsGetters.d.ts.map +1 -1
  56. package/lib/typescript/src/useScheduleUpdate.d.ts.map +1 -1
  57. package/package.json +3 -3
  58. package/src/BaseNavigationContainer.tsx +69 -31
  59. package/src/NavigationBuilderContext.tsx +27 -17
  60. package/src/NavigationIndependentTree.tsx +41 -13
  61. package/src/NavigationProvider.tsx +11 -8
  62. package/src/NavigationStateContext.tsx +0 -4
  63. package/src/SceneView.tsx +18 -25
  64. package/src/StaticNavigation.tsx +71 -17
  65. package/src/useDescriptors.tsx +5 -4
  66. package/src/useFocusedListenersChildrenAdapter.tsx +2 -2
  67. package/src/useNavigationBuilder.tsx +2 -4
  68. package/src/useNavigationCache.tsx +13 -30
  69. package/src/useNavigationHelpers.tsx +18 -6
  70. package/src/useNavigationState.tsx +1 -1
  71. package/src/useOnAction.tsx +2 -2
  72. package/src/useOnGetState.tsx +2 -2
  73. package/src/useOnPreventRemove.tsx +2 -2
  74. package/src/useOnRouteFocus.tsx +2 -4
  75. package/src/useOptionsGetters.tsx +9 -6
  76. package/src/useScheduleUpdate.tsx +2 -4
@@ -1,10 +1,20 @@
1
1
  import * as React from 'react';
2
2
 
3
+ import { NavigationBuilderContext } from './NavigationBuilderContext';
4
+ import { NavigationContainerRefContext } from './NavigationContainerRefContext';
3
5
  import { NavigationContext } from './NavigationContext';
4
6
  import { NavigationFocusedRouteStateContext } from './NavigationFocusedRouteStateContext';
7
+ import { NavigationHelpersContext } from './NavigationHelpersContext';
5
8
  import { NavigationIndependentTreeContext } from './NavigationIndependentTreeContext';
6
- import { NavigationRouteContext } from './NavigationProvider';
7
- import { IsFocusedContext } from './useIsFocused';
9
+ // eslint-disable-next-line import-x/no-deprecated
10
+ import { NavigationMetaContext } from './NavigationMetaContext';
11
+ import {
12
+ NamedRouteContextListContext,
13
+ NavigationRouteContext,
14
+ } from './NavigationProvider';
15
+ import { PreventRemoveContext } from './PreventRemoveContext';
16
+ import { FocusedRouteKeyContext, IsFocusedContext } from './useIsFocused';
17
+ import { NavigationStateListenerContext } from './useNavigationState';
8
18
 
9
19
  /**
10
20
  * Component to make the child navigation container independent of parent containers.
@@ -16,16 +26,34 @@ export function NavigationIndependentTree({
16
26
  }) {
17
27
  return (
18
28
  // We need to clear any existing contexts for nested independent container to work correctly
19
- <NavigationRouteContext.Provider value={undefined}>
20
- <NavigationContext.Provider value={undefined}>
21
- <NavigationFocusedRouteStateContext.Provider value={undefined}>
22
- <IsFocusedContext.Provider value={undefined}>
23
- <NavigationIndependentTreeContext.Provider value>
24
- {children}
25
- </NavigationIndependentTreeContext.Provider>
26
- </IsFocusedContext.Provider>
27
- </NavigationFocusedRouteStateContext.Provider>
28
- </NavigationContext.Provider>
29
- </NavigationRouteContext.Provider>
29
+ <NavigationIndependentTreeContext.Provider value>
30
+ <NavigationContainerRefContext.Provider value={undefined}>
31
+ <NavigationBuilderContext.Provider value={undefined}>
32
+ <NavigationMetaContext.Provider value={undefined}>
33
+ <NavigationHelpersContext.Provider value={undefined}>
34
+ <NavigationStateListenerContext.Provider value={undefined}>
35
+ <FocusedRouteKeyContext.Provider value={undefined}>
36
+ <PreventRemoveContext.Provider value={undefined}>
37
+ <NamedRouteContextListContext.Provider value={undefined}>
38
+ <NavigationRouteContext.Provider value={undefined}>
39
+ <NavigationContext.Provider value={undefined}>
40
+ <IsFocusedContext.Provider value={undefined}>
41
+ <NavigationFocusedRouteStateContext.Provider
42
+ value={undefined}
43
+ >
44
+ {children}
45
+ </NavigationFocusedRouteStateContext.Provider>
46
+ </IsFocusedContext.Provider>
47
+ </NavigationContext.Provider>
48
+ </NavigationRouteContext.Provider>
49
+ </NamedRouteContextListContext.Provider>
50
+ </PreventRemoveContext.Provider>
51
+ </FocusedRouteKeyContext.Provider>
52
+ </NavigationStateListenerContext.Provider>
53
+ </NavigationHelpersContext.Provider>
54
+ </NavigationMetaContext.Provider>
55
+ </NavigationBuilderContext.Provider>
56
+ </NavigationContainerRefContext.Provider>
57
+ </NavigationIndependentTreeContext.Provider>
30
58
  );
31
59
  }
@@ -37,13 +37,16 @@ export function NavigationProvider({ route, navigation, children }: Props) {
37
37
  ? focusedRouteKey === route.key
38
38
  : false;
39
39
 
40
- return (
41
- <NavigationRouteContext.Provider value={route}>
42
- <NavigationContext.Provider value={navigation}>
43
- <IsFocusedContext.Provider value={isFocused}>
44
- {children}
45
- </IsFocusedContext.Provider>
46
- </NavigationContext.Provider>
47
- </NavigationRouteContext.Provider>
40
+ return React.useMemo(
41
+ () => (
42
+ <NavigationRouteContext.Provider value={route}>
43
+ <NavigationContext.Provider value={navigation}>
44
+ <IsFocusedContext.Provider value={isFocused}>
45
+ {children}
46
+ </IsFocusedContext.Provider>
47
+ </NavigationContext.Provider>
48
+ </NavigationRouteContext.Provider>
49
+ ),
50
+ [children, isFocused, navigation, route]
48
51
  );
49
52
  }
@@ -14,10 +14,6 @@ export const NavigationStateContext = React.createContext<{
14
14
  state: NavigationState | PartialState<NavigationState> | undefined
15
15
  ) => void;
16
16
  getIsInitial: () => boolean;
17
- addOptionsGetter?: (
18
- key: string,
19
- getter: () => object | undefined | null
20
- ) => void;
21
17
  }>({
22
18
  isDefault: true,
23
19
 
package/src/SceneView.tsx CHANGED
@@ -15,7 +15,7 @@ import {
15
15
  import { NavigationStateContext } from './NavigationStateContext';
16
16
  import { StaticContainer } from './StaticContainer';
17
17
  import type { NavigationProp, RouteConfigComponent } from './types';
18
- import { useOptionsGetters } from './useOptionsGetters';
18
+ import { OptionsGetterContext, useOptionsGetters } from './useOptionsGetters';
19
19
 
20
20
  type Props<State extends NavigationState, ScreenOptions extends {}> = {
21
21
  screen: RouteConfigComponent<ParamListBase, string> & { name: string };
@@ -163,17 +163,8 @@ export function SceneView<
163
163
  getKey,
164
164
  setKey,
165
165
  getIsInitial,
166
- addOptionsGetter,
167
166
  }),
168
- [
169
- routeState,
170
- getCurrentState,
171
- setCurrentState,
172
- getKey,
173
- setKey,
174
- getIsInitial,
175
- addOptionsGetter,
176
- ]
167
+ [routeState, getCurrentState, setCurrentState, getKey, setKey, getIsInitial]
177
168
  );
178
169
 
179
170
  const ScreenComponent = screen.getComponent
@@ -183,20 +174,22 @@ export function SceneView<
183
174
  return (
184
175
  <NavigationStateContext.Provider value={context}>
185
176
  <NavigationFocusedRouteStateContext.Provider value={focusedRouteState}>
186
- <EnsureSingleNavigator>
187
- <StaticContainer
188
- name={screen.name}
189
- render={ScreenComponent || screen.children}
190
- navigation={navigation}
191
- route={route}
192
- >
193
- {ScreenComponent !== undefined ? (
194
- <ScreenComponent navigation={navigation} route={route} />
195
- ) : screen.children !== undefined ? (
196
- screen.children({ navigation, route })
197
- ) : null}
198
- </StaticContainer>
199
- </EnsureSingleNavigator>
177
+ <OptionsGetterContext.Provider value={addOptionsGetter}>
178
+ <EnsureSingleNavigator>
179
+ <StaticContainer
180
+ name={screen.name}
181
+ render={ScreenComponent || screen.children}
182
+ navigation={navigation}
183
+ route={route}
184
+ >
185
+ {ScreenComponent !== undefined ? (
186
+ <ScreenComponent navigation={navigation} route={route} />
187
+ ) : screen.children !== undefined ? (
188
+ screen.children({ navigation, route })
189
+ ) : null}
190
+ </StaticContainer>
191
+ </EnsureSingleNavigator>
192
+ </OptionsGetterContext.Provider>
200
193
  </NavigationFocusedRouteStateContext.Provider>
201
194
  </NavigationStateContext.Provider>
202
195
  );
@@ -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
  ) {
@@ -13,6 +13,7 @@ import {
13
13
  type AddKeyedListener,
14
14
  type AddListener,
15
15
  NavigationBuilderContext,
16
+ useNavigationBuilderContext,
16
17
  } from './NavigationBuilderContext';
17
18
  import { NavigationProvider } from './NavigationProvider';
18
19
  import { SceneView } from './SceneView';
@@ -149,9 +150,9 @@ export function useDescriptors<
149
150
  onOptionsChange,
150
151
  scheduleUpdate,
151
152
  flushUpdates,
152
- stackRef,
153
+ withStackTrace,
153
154
  getIsStateEmitted,
154
- } = React.useContext(NavigationBuilderContext);
155
+ } = useNavigationBuilderContext();
155
156
 
156
157
  const context = React.useMemo(
157
158
  () => ({
@@ -166,7 +167,7 @@ export function useDescriptors<
166
167
  getIsStateEmitted,
167
168
  scheduleUpdate,
168
169
  flushUpdates,
169
- stackRef,
170
+ withStackTrace,
170
171
  }),
171
172
  [
172
173
  navigation,
@@ -180,7 +181,7 @@ export function useDescriptors<
180
181
  getIsStateEmitted,
181
182
  scheduleUpdate,
182
183
  flushUpdates,
183
- stackRef,
184
+ withStackTrace,
184
185
  ]
185
186
  );
186
187
 
@@ -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,7 +19,7 @@ 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
24
  // eslint-disable-next-line import-x/no-deprecated
25
25
  import { NavigationMetaContext } from './NavigationMetaContext';
@@ -463,9 +463,7 @@ export function useNavigationBuilder<
463
463
  getIsInitial,
464
464
  } = React.useContext(NavigationStateContext);
465
465
 
466
- const { onEmitEvent, getIsStateEmitted } = React.useContext(
467
- NavigationBuilderContext
468
- );
466
+ const { onEmitEvent, getIsStateEmitted } = useNavigationBuilderContext();
469
467
 
470
468
  const stateCleanupRef = React.useRef<boolean>(false);
471
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,6 +1,6 @@
1
1
  import * as React from 'react';
2
2
 
3
- import { NavigationBuilderContext } from './NavigationBuilderContext';
3
+ import { useNavigationBuilderContext } from './NavigationBuilderContext';
4
4
  import { useClientLayoutEffect } from './useClientLayoutEffect';
5
5
 
6
6
  /**
@@ -12,9 +12,7 @@ import { useClientLayoutEffect } from './useClientLayoutEffect';
12
12
  * This lets nested updates be applied from the root down without clobbering each other.
13
13
  */
14
14
  export function useScheduleUpdate(callback: () => void) {
15
- const { scheduleUpdate, flushUpdates } = React.useContext(
16
- NavigationBuilderContext
17
- );
15
+ const { scheduleUpdate, flushUpdates } = useNavigationBuilderContext();
18
16
 
19
17
  React.useInsertionEffect(() => {
20
18
  scheduleUpdate(callback);