@react-navigation/core 8.0.0-alpha.4 → 8.0.0-alpha.40

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 (323) hide show
  1. package/README.md +17 -14
  2. package/lib/module/BaseNavigationContainer.js +168 -87
  3. package/lib/module/BaseNavigationContainer.js.map +1 -1
  4. package/lib/module/ConsumedParamsContext.js +5 -0
  5. package/lib/module/ConsumedParamsContext.js.map +1 -0
  6. package/lib/module/DataLoading.js +129 -0
  7. package/lib/module/DataLoading.js.map +1 -0
  8. package/lib/module/NavigationBuilderContext.js +7 -10
  9. package/lib/module/NavigationBuilderContext.js.map +1 -1
  10. package/lib/module/NavigationIndependentTree.js +54 -9
  11. package/lib/module/NavigationIndependentTree.js.map +1 -1
  12. package/lib/module/NavigationProvider.js +36 -11
  13. package/lib/module/NavigationProvider.js.map +1 -1
  14. package/lib/module/NavigationRootContext.js +8 -0
  15. package/lib/module/NavigationRootContext.js.map +1 -0
  16. package/lib/module/NavigationStateContext.js +3 -0
  17. package/lib/module/NavigationStateContext.js.map +1 -1
  18. package/lib/module/PreventRemoveContext.js.map +1 -1
  19. package/lib/module/PreventRemoveProvider.js +40 -31
  20. package/lib/module/PreventRemoveProvider.js.map +1 -1
  21. package/lib/module/SceneView.js +12 -42
  22. package/lib/module/SceneView.js.map +1 -1
  23. package/lib/module/StaticNavigation.js +219 -67
  24. package/lib/module/StaticNavigation.js.map +1 -1
  25. package/lib/module/StaticTreeContext.js +5 -0
  26. package/lib/module/StaticTreeContext.js.map +1 -0
  27. package/lib/module/createNavigationContainerRef.js +7 -9
  28. package/lib/module/createNavigationContainerRef.js.map +1 -1
  29. package/lib/module/createNavigatorFactory.js +27 -5
  30. package/lib/module/createNavigatorFactory.js.map +1 -1
  31. package/lib/module/findFocusedRoute.js +7 -4
  32. package/lib/module/findFocusedRoute.js.map +1 -1
  33. package/lib/module/getActionFromState.js +45 -20
  34. package/lib/module/getActionFromState.js.map +1 -1
  35. package/lib/module/getPathFromState.js +204 -94
  36. package/lib/module/getPathFromState.js.map +1 -1
  37. package/lib/module/getPatternParts.js +61 -14
  38. package/lib/module/getPatternParts.js.map +1 -1
  39. package/lib/module/getStateFromPath.js +608 -272
  40. package/lib/module/getStateFromPath.js.map +1 -1
  41. package/lib/module/getStateFromRouteParams.js +23 -0
  42. package/lib/module/getStateFromRouteParams.js.map +1 -0
  43. package/lib/module/index.js +7 -6
  44. package/lib/module/index.js.map +1 -1
  45. package/lib/module/internal.js +2 -0
  46. package/lib/module/internal.js.map +1 -0
  47. package/lib/module/isRecordEqual.js +1 -1
  48. package/lib/module/isRecordEqual.js.map +1 -1
  49. package/lib/module/queryString.js +71 -0
  50. package/lib/module/queryString.js.map +1 -0
  51. package/lib/module/theming/useTheme.js +1 -1
  52. package/lib/module/theming/useTheme.js.map +1 -1
  53. package/lib/module/types.js +35 -1
  54. package/lib/module/types.js.map +1 -1
  55. package/lib/module/useDescriptors.js +65 -25
  56. package/lib/module/useDescriptors.js.map +1 -1
  57. package/lib/module/useEventEmitter.js +48 -33
  58. package/lib/module/useEventEmitter.js.map +1 -1
  59. package/lib/module/useFocusEffect.js +4 -2
  60. package/lib/module/useFocusEffect.js.map +1 -1
  61. package/lib/module/useFocusEvents.js +15 -5
  62. package/lib/module/useFocusEvents.js.map +1 -1
  63. package/lib/module/useFocusedListenersChildrenAdapter.js +3 -3
  64. package/lib/module/useFocusedListenersChildrenAdapter.js.map +1 -1
  65. package/lib/module/useIsFocused.js +8 -12
  66. package/lib/module/useIsFocused.js.map +1 -1
  67. package/lib/module/useKeyedChildListeners.js +5 -2
  68. package/lib/module/useKeyedChildListeners.js.map +1 -1
  69. package/lib/module/useLazyValue.js.map +1 -1
  70. package/lib/module/useNavigation.js +4 -17
  71. package/lib/module/useNavigation.js.map +1 -1
  72. package/lib/module/useNavigationBuilder.js +244 -147
  73. package/lib/module/useNavigationBuilder.js.map +1 -1
  74. package/lib/module/useNavigationCache.js +92 -89
  75. package/lib/module/useNavigationCache.js.map +1 -1
  76. package/lib/module/useNavigationHelpers.js +22 -29
  77. package/lib/module/useNavigationHelpers.js.map +1 -1
  78. package/lib/module/useNavigationIndependentTree.js +1 -1
  79. package/lib/module/useNavigationIndependentTree.js.map +1 -1
  80. package/lib/module/useNavigationState.js +158 -46
  81. package/lib/module/useNavigationState.js.map +1 -1
  82. package/lib/module/useOnAction.js +58 -11
  83. package/lib/module/useOnAction.js.map +1 -1
  84. package/lib/module/useOnGetNavigation.js +26 -0
  85. package/lib/module/useOnGetNavigation.js.map +1 -0
  86. package/lib/module/useOnGetState.js +7 -4
  87. package/lib/module/useOnGetState.js.map +1 -1
  88. package/lib/module/useOnPreventRemove.js +40 -31
  89. package/lib/module/useOnPreventRemove.js.map +1 -1
  90. package/lib/module/useOnRouteFocus.js +2 -2
  91. package/lib/module/useOnRouteFocus.js.map +1 -1
  92. package/lib/module/useOptionsGetters.js +34 -24
  93. package/lib/module/useOptionsGetters.js.map +1 -1
  94. package/lib/module/usePreventRemove.js +11 -5
  95. package/lib/module/usePreventRemove.js.map +1 -1
  96. package/lib/module/usePreventRemoveContext.js +2 -2
  97. package/lib/module/usePreventRemoveContext.js.map +1 -1
  98. package/lib/module/useRegisterNavigator.js +3 -4
  99. package/lib/module/useRegisterNavigator.js.map +1 -1
  100. package/lib/module/useRoute.js +2 -0
  101. package/lib/module/useRoute.js.map +1 -1
  102. package/lib/module/useRouteCache.js +51 -35
  103. package/lib/module/useRouteCache.js.map +1 -1
  104. package/lib/module/useScheduleUpdate.js +12 -10
  105. package/lib/module/useScheduleUpdate.js.map +1 -1
  106. package/lib/module/useStateForPath.js +1 -1
  107. package/lib/module/useStateForPath.js.map +1 -1
  108. package/lib/module/useSyncState.js +37 -15
  109. package/lib/module/useSyncState.js.map +1 -1
  110. package/lib/module/validatePathConfig.js +50 -36
  111. package/lib/module/validatePathConfig.js.map +1 -1
  112. package/lib/typescript/src/BaseNavigationContainer.d.ts +6 -3
  113. package/lib/typescript/src/BaseNavigationContainer.d.ts.map +1 -1
  114. package/lib/typescript/src/ConsumedParamsContext.d.ts +3 -0
  115. package/lib/typescript/src/ConsumedParamsContext.d.ts.map +1 -0
  116. package/lib/typescript/src/DataLoading.d.ts +21 -0
  117. package/lib/typescript/src/DataLoading.d.ts.map +1 -0
  118. package/lib/typescript/src/EnsureSingleNavigator.d.ts +1 -1
  119. package/lib/typescript/src/EnsureSingleNavigator.d.ts.map +1 -1
  120. package/lib/typescript/src/Group.d.ts +1 -1
  121. package/lib/typescript/src/Group.d.ts.map +1 -1
  122. package/lib/typescript/src/NavigationBuilderContext.d.ts +22 -14
  123. package/lib/typescript/src/NavigationBuilderContext.d.ts.map +1 -1
  124. package/lib/typescript/src/NavigationContainerRefContext.d.ts +1 -1
  125. package/lib/typescript/src/NavigationContainerRefContext.d.ts.map +1 -1
  126. package/lib/typescript/src/NavigationFocusedRouteStateContext.d.ts +4 -4
  127. package/lib/typescript/src/NavigationFocusedRouteStateContext.d.ts.map +1 -1
  128. package/lib/typescript/src/NavigationHelpersContext.d.ts +1 -1
  129. package/lib/typescript/src/NavigationHelpersContext.d.ts.map +1 -1
  130. package/lib/typescript/src/NavigationIndependentTree.d.ts +1 -1
  131. package/lib/typescript/src/NavigationIndependentTree.d.ts.map +1 -1
  132. package/lib/typescript/src/NavigationProvider.d.ts +21 -18
  133. package/lib/typescript/src/NavigationProvider.d.ts.map +1 -1
  134. package/lib/typescript/src/NavigationRootContext.d.ts +8 -0
  135. package/lib/typescript/src/NavigationRootContext.d.ts.map +1 -0
  136. package/lib/typescript/src/NavigationStateContext.d.ts +4 -3
  137. package/lib/typescript/src/NavigationStateContext.d.ts.map +1 -1
  138. package/lib/typescript/src/PreventRemoveContext.d.ts +1 -0
  139. package/lib/typescript/src/PreventRemoveContext.d.ts.map +1 -1
  140. package/lib/typescript/src/PreventRemoveProvider.d.ts +1 -1
  141. package/lib/typescript/src/PreventRemoveProvider.d.ts.map +1 -1
  142. package/lib/typescript/src/SceneView.d.ts +5 -2
  143. package/lib/typescript/src/SceneView.d.ts.map +1 -1
  144. package/lib/typescript/src/Screen.d.ts +1 -1
  145. package/lib/typescript/src/Screen.d.ts.map +1 -1
  146. package/lib/typescript/src/StaticContainer.d.ts +1 -1
  147. package/lib/typescript/src/StaticContainer.d.ts.map +1 -1
  148. package/lib/typescript/src/StaticNavigation.d.ts +189 -111
  149. package/lib/typescript/src/StaticNavigation.d.ts.map +1 -1
  150. package/lib/typescript/src/StaticTreeContext.d.ts +4 -0
  151. package/lib/typescript/src/StaticTreeContext.d.ts.map +1 -0
  152. package/lib/typescript/src/createNavigationContainerRef.d.ts +1 -1
  153. package/lib/typescript/src/createNavigationContainerRef.d.ts.map +1 -1
  154. package/lib/typescript/src/createNavigatorFactory.d.ts +23 -5
  155. package/lib/typescript/src/createNavigatorFactory.d.ts.map +1 -1
  156. package/lib/typescript/src/findFocusedRoute.d.ts +3 -8
  157. package/lib/typescript/src/findFocusedRoute.d.ts.map +1 -1
  158. package/lib/typescript/src/getActionFromState.d.ts +4 -4
  159. package/lib/typescript/src/getActionFromState.d.ts.map +1 -1
  160. package/lib/typescript/src/getPathFromState.d.ts +3 -3
  161. package/lib/typescript/src/getPathFromState.d.ts.map +1 -1
  162. package/lib/typescript/src/getPatternParts.d.ts +5 -2
  163. package/lib/typescript/src/getPatternParts.d.ts.map +1 -1
  164. package/lib/typescript/src/getStateFromPath.d.ts +4 -7
  165. package/lib/typescript/src/getStateFromPath.d.ts.map +1 -1
  166. package/lib/typescript/src/getStateFromRouteParams.d.ts +3 -0
  167. package/lib/typescript/src/getStateFromRouteParams.d.ts.map +1 -0
  168. package/lib/typescript/src/index.d.ts +36 -33
  169. package/lib/typescript/src/index.d.ts.map +1 -1
  170. package/lib/typescript/src/internal.d.ts +3 -0
  171. package/lib/typescript/src/internal.d.ts.map +1 -0
  172. package/lib/typescript/src/isRecordEqual.d.ts.map +1 -1
  173. package/lib/typescript/src/queryString.d.ts +5 -0
  174. package/lib/typescript/src/queryString.d.ts.map +1 -0
  175. package/lib/typescript/src/theming/ThemeContext.d.ts +1 -1
  176. package/lib/typescript/src/theming/ThemeContext.d.ts.map +1 -1
  177. package/lib/typescript/src/theming/ThemeProvider.d.ts +2 -2
  178. package/lib/typescript/src/theming/ThemeProvider.d.ts.map +1 -1
  179. package/lib/typescript/src/theming/useTheme.d.ts +1 -1
  180. package/lib/typescript/src/theming/useTheme.d.ts.map +1 -1
  181. package/lib/typescript/src/types.d.ts +265 -167
  182. package/lib/typescript/src/types.d.ts.map +1 -1
  183. package/lib/typescript/src/useChildListeners.d.ts +3 -3
  184. package/lib/typescript/src/useChildListeners.d.ts.map +1 -1
  185. package/lib/typescript/src/useDescriptors.d.ts +13 -11
  186. package/lib/typescript/src/useDescriptors.d.ts.map +1 -1
  187. package/lib/typescript/src/useEventEmitter.d.ts +2 -2
  188. package/lib/typescript/src/useEventEmitter.d.ts.map +1 -1
  189. package/lib/typescript/src/useFocusEffect.d.ts.map +1 -1
  190. package/lib/typescript/src/useFocusEvents.d.ts +2 -2
  191. package/lib/typescript/src/useFocusEvents.d.ts.map +1 -1
  192. package/lib/typescript/src/useFocusedListenersChildrenAdapter.d.ts +2 -2
  193. package/lib/typescript/src/useFocusedListenersChildrenAdapter.d.ts.map +1 -1
  194. package/lib/typescript/src/useIsFocused.d.ts +4 -0
  195. package/lib/typescript/src/useIsFocused.d.ts.map +1 -1
  196. package/lib/typescript/src/useKeyedChildListeners.d.ts +4 -3
  197. package/lib/typescript/src/useKeyedChildListeners.d.ts.map +1 -1
  198. package/lib/typescript/src/useLazyValue.d.ts.map +1 -1
  199. package/lib/typescript/src/useNavigation.d.ts +4 -2
  200. package/lib/typescript/src/useNavigation.d.ts.map +1 -1
  201. package/lib/typescript/src/useNavigationBuilder.d.ts +22 -63
  202. package/lib/typescript/src/useNavigationBuilder.d.ts.map +1 -1
  203. package/lib/typescript/src/useNavigationCache.d.ts +6 -5
  204. package/lib/typescript/src/useNavigationCache.d.ts.map +1 -1
  205. package/lib/typescript/src/useNavigationContainerRef.d.ts +1 -1
  206. package/lib/typescript/src/useNavigationContainerRef.d.ts.map +1 -1
  207. package/lib/typescript/src/useNavigationHelpers.d.ts +18 -58
  208. package/lib/typescript/src/useNavigationHelpers.d.ts.map +1 -1
  209. package/lib/typescript/src/useNavigationState.d.ts +19 -3
  210. package/lib/typescript/src/useNavigationState.d.ts.map +1 -1
  211. package/lib/typescript/src/useOnAction.d.ts +9 -9
  212. package/lib/typescript/src/useOnAction.d.ts.map +1 -1
  213. package/lib/typescript/src/useOnGetNavigation.d.ts +12 -0
  214. package/lib/typescript/src/useOnGetNavigation.d.ts.map +1 -0
  215. package/lib/typescript/src/useOnGetState.d.ts +1 -1
  216. package/lib/typescript/src/useOnGetState.d.ts.map +1 -1
  217. package/lib/typescript/src/useOnPreventRemove.d.ts +5 -9
  218. package/lib/typescript/src/useOnPreventRemove.d.ts.map +1 -1
  219. package/lib/typescript/src/useOnRouteFocus.d.ts +6 -6
  220. package/lib/typescript/src/useOnRouteFocus.d.ts.map +1 -1
  221. package/lib/typescript/src/useOptionsGetters.d.ts +2 -4
  222. package/lib/typescript/src/useOptionsGetters.d.ts.map +1 -1
  223. package/lib/typescript/src/usePreventRemove.d.ts.map +1 -1
  224. package/lib/typescript/src/usePreventRemoveContext.d.ts +2 -1
  225. package/lib/typescript/src/usePreventRemoveContext.d.ts.map +1 -1
  226. package/lib/typescript/src/useRegisterNavigator.d.ts.map +1 -1
  227. package/lib/typescript/src/useRoute.d.ts +7 -3
  228. package/lib/typescript/src/useRoute.d.ts.map +1 -1
  229. package/lib/typescript/src/useRouteCache.d.ts +7 -12
  230. package/lib/typescript/src/useRouteCache.d.ts.map +1 -1
  231. package/lib/typescript/src/useScheduleUpdate.d.ts +5 -4
  232. package/lib/typescript/src/useScheduleUpdate.d.ts.map +1 -1
  233. package/lib/typescript/src/useStateForPath.d.ts +1 -1
  234. package/lib/typescript/src/useStateForPath.d.ts.map +1 -1
  235. package/lib/typescript/src/useSyncState.d.ts +1 -0
  236. package/lib/typescript/src/useSyncState.d.ts.map +1 -1
  237. package/lib/typescript/src/utilities.d.ts +79 -39
  238. package/lib/typescript/src/utilities.d.ts.map +1 -1
  239. package/lib/typescript/src/validatePathConfig.d.ts.map +1 -1
  240. package/package.json +26 -22
  241. package/src/BaseNavigationContainer.tsx +468 -333
  242. package/src/ConsumedParamsContext.tsx +5 -0
  243. package/src/DataLoading.tsx +206 -0
  244. package/src/NavigationBuilderContext.tsx +44 -25
  245. package/src/NavigationFocusedRouteStateContext.tsx +4 -4
  246. package/src/NavigationIndependentTree.tsx +60 -12
  247. package/src/NavigationProvider.tsx +47 -20
  248. package/src/NavigationRootContext.tsx +11 -0
  249. package/src/NavigationStateContext.tsx +9 -6
  250. package/src/PreventRemoveContext.tsx +1 -0
  251. package/src/PreventRemoveProvider.tsx +55 -56
  252. package/src/SceneView.tsx +14 -40
  253. package/src/StaticNavigation.tsx +715 -328
  254. package/src/StaticTreeContext.tsx +7 -0
  255. package/src/createNavigationContainerRef.tsx +12 -11
  256. package/src/createNavigatorFactory.tsx +92 -8
  257. package/src/findFocusedRoute.tsx +16 -16
  258. package/src/getActionFromState.tsx +87 -51
  259. package/src/getPathFromState.tsx +310 -134
  260. package/src/getPatternParts.tsx +98 -16
  261. package/src/getStateFromPath.tsx +971 -344
  262. package/src/getStateFromRouteParams.tsx +59 -0
  263. package/src/index.tsx +57 -6
  264. package/src/internal.tsx +2 -0
  265. package/src/isRecordEqual.tsx +3 -1
  266. package/src/queryString.ts +109 -0
  267. package/src/theming/useTheme.tsx +1 -1
  268. package/src/types.tsx +799 -377
  269. package/src/useDescriptors.tsx +151 -43
  270. package/src/useEventEmitter.tsx +53 -39
  271. package/src/useFocusEffect.tsx +5 -2
  272. package/src/useFocusEvents.tsx +19 -8
  273. package/src/useFocusedListenersChildrenAdapter.tsx +3 -3
  274. package/src/useIsFocused.tsx +18 -21
  275. package/src/useKeyedChildListeners.tsx +5 -2
  276. package/src/useLazyValue.tsx +2 -2
  277. package/src/useNavigation.tsx +9 -26
  278. package/src/useNavigationBuilder.tsx +370 -201
  279. package/src/useNavigationCache.tsx +148 -123
  280. package/src/useNavigationHelpers.tsx +27 -37
  281. package/src/useNavigationIndependentTree.tsx +1 -1
  282. package/src/useNavigationState.tsx +207 -69
  283. package/src/useOnAction.tsx +92 -18
  284. package/src/useOnGetNavigation.tsx +30 -0
  285. package/src/useOnGetState.tsx +8 -4
  286. package/src/useOnPreventRemove.tsx +78 -43
  287. package/src/useOnRouteFocus.tsx +11 -15
  288. package/src/useOptionsGetters.tsx +41 -28
  289. package/src/usePreventRemove.tsx +13 -5
  290. package/src/usePreventRemoveContext.tsx +2 -2
  291. package/src/useRegisterNavigator.tsx +4 -4
  292. package/src/useRoute.tsx +20 -6
  293. package/src/useRouteCache.tsx +57 -35
  294. package/src/useScheduleUpdate.tsx +13 -11
  295. package/src/useStateForPath.tsx +1 -1
  296. package/src/useSyncState.tsx +39 -15
  297. package/src/utilities.tsx +151 -35
  298. package/src/validatePathConfig.tsx +86 -57
  299. package/lib/module/CurrentRenderContext.js +0 -10
  300. package/lib/module/CurrentRenderContext.js.map +0 -1
  301. package/lib/module/NavigationMetaContext.js +0 -15
  302. package/lib/module/NavigationMetaContext.js.map +0 -1
  303. package/lib/module/checkSerializable.js +0 -46
  304. package/lib/module/checkSerializable.js.map +0 -1
  305. package/lib/module/useComponent.js +0 -34
  306. package/lib/module/useComponent.js.map +0 -1
  307. package/lib/module/useCurrentRender.js +0 -19
  308. package/lib/module/useCurrentRender.js.map +0 -1
  309. package/lib/typescript/src/CurrentRenderContext.d.ts +0 -9
  310. package/lib/typescript/src/CurrentRenderContext.d.ts.map +0 -1
  311. package/lib/typescript/src/NavigationMetaContext.d.ts +0 -12
  312. package/lib/typescript/src/NavigationMetaContext.d.ts.map +0 -1
  313. package/lib/typescript/src/checkSerializable.d.ts +0 -10
  314. package/lib/typescript/src/checkSerializable.d.ts.map +0 -1
  315. package/lib/typescript/src/useComponent.d.ts +0 -7
  316. package/lib/typescript/src/useComponent.d.ts.map +0 -1
  317. package/lib/typescript/src/useCurrentRender.d.ts +0 -14
  318. package/lib/typescript/src/useCurrentRender.d.ts.map +0 -1
  319. package/src/CurrentRenderContext.tsx +0 -9
  320. package/src/NavigationMetaContext.tsx +0 -14
  321. package/src/checkSerializable.tsx +0 -74
  322. package/src/useComponent.tsx +0 -37
  323. package/src/useCurrentRender.tsx +0 -31
@@ -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 { NavigationContext } from './NavigationProvider';
12
12
  import type { NavigationHelpers, NavigationProp } from './types';
13
13
  import type { NavigationEventEmitter } from './useEventEmitter';
@@ -19,12 +19,13 @@ type Options<
19
19
  > = {
20
20
  routes: State['routes'];
21
21
  getState: () => State;
22
- navigation: NavigationHelpers<ParamListBase> &
22
+ navigation: NavigationHelpers<ParamListBase, State> &
23
23
  Partial<NavigationProp<ParamListBase, string, any, any, any>>;
24
+ canGoBack: (source?: string) => boolean;
24
25
  setOptions: (
25
26
  cb: (
26
- options: Record<string, ScreenOptions>
27
- ) => Record<string, ScreenOptions>
27
+ options: Record<string, Partial<ScreenOptions> | undefined>
28
+ ) => Record<string, Partial<ScreenOptions> | undefined>
28
29
  ) => void;
29
30
  router: Router<State, NavigationAction>;
30
31
  emitter: NavigationEventEmitter<EventMap>;
@@ -68,134 +69,158 @@ export function useNavigationCache<
68
69
  routes,
69
70
  getState,
70
71
  navigation,
72
+ canGoBack,
71
73
  setOptions,
72
74
  router,
73
75
  emitter,
74
76
  }: Options<State, ScreenOptions, EventMap>) {
75
- const parentNavigationHelpers = React.useContext(NavigationContext);
76
- const { stackRef } = React.useContext(NavigationBuilderContext);
77
+ const { withStackTrace } = useNavigationBuilderContext();
78
+
79
+ const parentNavigation = React.use(NavigationContext);
77
80
 
78
81
  // Cache object which holds navigation objects for each screen
79
82
  // We use `React.useMemo` instead of `React.useRef` coz we want to invalidate it when deps change
80
83
  // In reality, these deps will rarely change, if ever
81
- const cache = React.useMemo(
82
- () => ({
83
- current: {} as NavigationCache<
84
- State,
85
- ScreenOptions,
86
- EventMap,
87
- ActionHelpers
88
- >,
89
- }),
84
+ const cache = React.useMemo<{
85
+ current: NavigationCache<State, ScreenOptions, EventMap, ActionHelpers>;
86
+ routeNames: Record<string, string>;
87
+ }>(
88
+ () => ({ current: {}, routeNames: {} }),
90
89
  // eslint-disable-next-line react-hooks/exhaustive-deps
91
- [getState, parentNavigationHelpers, navigation, setOptions, emitter]
90
+ [
91
+ getState,
92
+ parentNavigation,
93
+ navigation,
94
+ canGoBack,
95
+ setOptions,
96
+ router,
97
+ emitter,
98
+ withStackTrace,
99
+ ]
92
100
  );
93
101
 
94
- cache.current = routes.reduce<
95
- NavigationCache<State, ScreenOptions, EventMap, ActionHelpers>
96
- >((acc, route) => {
97
- const previous = cache.current[route.key];
98
-
99
- type Thunk =
100
- | NavigationAction
101
- | ((state: State) => NavigationAction | null | undefined);
102
-
103
- if (previous) {
104
- // If a cached navigation object already exists, reuse it
105
- acc[route.key] = previous;
106
- } else {
107
- const dispatch = (thunk: Thunk) => {
108
- const action = typeof thunk === 'function' ? thunk(getState()) : thunk;
109
-
110
- if (action != null) {
111
- navigation.dispatch({ source: route.key, ...action });
112
- }
113
- };
114
-
115
- const withStack = (callback: () => void) => {
116
- let isStackSet = false;
117
-
118
- try {
119
- if (
120
- process.env.NODE_ENV !== 'production' &&
121
- stackRef &&
122
- !stackRef.current
123
- ) {
124
- // Capture the stack trace for devtools
125
- stackRef.current = new Error().stack;
126
- isStackSet = true;
127
- }
128
-
129
- callback();
130
- } finally {
131
- if (isStackSet && stackRef) {
132
- stackRef.current = undefined;
133
- }
134
- }
135
- };
136
-
137
- const actions = {
138
- ...router.actionCreators,
139
- ...CommonActions,
140
- };
141
-
142
- const helpers = Object.keys(actions).reduce<Record<string, () => void>>(
143
- (acc, name) => {
144
- acc[name] = (...args: any) =>
145
- withStack(() =>
146
- // @ts-expect-error: name is a valid key, but TypeScript is dumb
147
- dispatch(actions[name](...args))
148
- );
149
-
150
- return acc;
151
- },
152
- {}
153
- );
154
-
155
- // eslint-disable-next-line @typescript-eslint/no-unused-vars
156
- const { emit, ...rest } = navigation;
157
-
158
- acc[route.key] = {
159
- ...rest,
160
- ...helpers,
161
- // FIXME: too much work to fix the types for now
162
- ...(emitter.create(route.key) as any),
163
- dispatch: (thunk: Thunk) => withStack(() => dispatch(thunk)),
164
- getParent: (routeName) => {
165
- if (routeName === route.name) {
166
- // If the passed route name is the same as the current route's name,
167
- // we return the cached navigation object for the route
168
- return acc[route.key];
169
- }
170
-
171
- if (routeName !== undefined) {
172
- return parentNavigationHelpers?.getParent(routeName);
173
- }
174
-
175
- return parentNavigationHelpers;
176
- },
177
- setOptions: (options: object) => {
178
- setOptions((o) => ({
179
- ...o,
180
- [route.key]: { ...o[route.key], ...options },
181
- }));
182
- },
183
- isFocused: () => {
184
- const state = rest.getState();
185
-
186
- if (state.routes[state.index].key !== route.key) {
187
- return false;
188
- }
189
-
190
- // If the current screen is focused, we also need to check if parent navigator is focused
191
- // This makes sure that we return the focus state in the whole tree, not just this navigator
192
- return navigation ? navigation.isFocused() : true;
193
- },
194
- };
195
- }
196
-
197
- return acc;
198
- }, {});
199
-
200
- return cache.current;
102
+ const next = React.useMemo(() => {
103
+ return routes.reduce<
104
+ NavigationCache<State, ScreenOptions, EventMap, ActionHelpers>
105
+ >((acc, route) => {
106
+ const previous =
107
+ cache.routeNames[route.key] === route.name
108
+ ? cache.current[route.key]
109
+ : undefined;
110
+
111
+ if (previous) {
112
+ // If a cached navigation object already exists, reuse it
113
+ acc[route.key] = previous;
114
+ } else {
115
+ const dispatch = (
116
+ thunk:
117
+ | NavigationAction
118
+ | ((state: State) => NavigationAction | null | undefined)
119
+ ) => {
120
+ withStackTrace(dispatch, () => {
121
+ React.startTransition(() => {
122
+ const action =
123
+ typeof thunk === 'function' ? thunk(getState()) : thunk;
124
+
125
+ if (action != null) {
126
+ navigation.dispatch({ source: route.key, ...action });
127
+ }
128
+ });
129
+ });
130
+ };
131
+
132
+ const actions = {
133
+ ...router.actionCreators,
134
+ ...CommonActions,
135
+ };
136
+
137
+ const helpers = Object.keys(actions).reduce<Record<string, () => void>>(
138
+ (acc, name) => {
139
+ const helper = (...args: any) =>
140
+ withStackTrace(helper, () =>
141
+ // @ts-expect-error: name is a valid key, but TypeScript is dumb
142
+ dispatch(actions[name](...args))
143
+ );
144
+
145
+ acc[name] = helper;
146
+
147
+ return acc;
148
+ },
149
+ {}
150
+ );
151
+
152
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
153
+ const { emit, ...rest } = navigation;
154
+
155
+ acc[route.key] = {
156
+ ...rest,
157
+ ...helpers,
158
+ // FIXME: too much work to fix the types for now
159
+ ...(emitter.create(route.key) as any),
160
+ dispatch,
161
+ canGoBack: () => canGoBack(route.key),
162
+ getParent: (routeName) => {
163
+ if (routeName === route.name) {
164
+ // If the passed route name is the same as the current route's name,
165
+ // we return the cached navigation object for the route
166
+ return acc[route.key];
167
+ }
168
+
169
+ if (routeName !== undefined) {
170
+ const parent = parentNavigation?.getParent(routeName);
171
+
172
+ if (parent === undefined) {
173
+ throw new Error(
174
+ `Couldn't find a navigation object for '${routeName}' in current or any parent screens. Is your component inside the correct screen?`
175
+ );
176
+ }
177
+
178
+ return parent;
179
+ }
180
+
181
+ return parentNavigation;
182
+ },
183
+ setOptions: (options: Partial<ScreenOptions>) => {
184
+ setOptions((o) => ({
185
+ ...o,
186
+ [route.key]: { ...o[route.key], ...options },
187
+ }));
188
+ },
189
+ isFocused: () => {
190
+ const state = rest.getState();
191
+
192
+ if (state.routes[state.index]?.key !== route.key) {
193
+ return false;
194
+ }
195
+
196
+ // If the current screen is focused, we also need to check if parent navigator is focused
197
+ // This makes sure that we return the focus state in the whole tree, not just this navigator
198
+ return navigation ? navigation.isFocused() : true;
199
+ },
200
+ };
201
+ }
202
+
203
+ return acc;
204
+ }, {});
205
+ }, [
206
+ cache,
207
+ routes,
208
+ getState,
209
+ navigation,
210
+ canGoBack,
211
+ router,
212
+ withStackTrace,
213
+ emitter,
214
+ parentNavigation,
215
+ setOptions,
216
+ ]);
217
+
218
+ React.useInsertionEffect(() => {
219
+ cache.current = next;
220
+ cache.routeNames = Object.fromEntries(
221
+ routes.map((route) => [route.key, route.name])
222
+ );
223
+ }, [cache, next, routes]);
224
+
225
+ return next;
201
226
  }
@@ -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 './NavigationProvider';
11
12
  import { type NavigationHelpers, PrivateValueStore } from './types';
12
13
  import type { NavigationEventEmitter } from './useEventEmitter';
@@ -18,10 +19,10 @@ PrivateValueStore;
18
19
  type Options<State extends NavigationState, Action extends NavigationAction> = {
19
20
  onAction: (action: NavigationAction) => boolean;
20
21
  onUnhandledAction: (action: NavigationAction) => void;
22
+ canGoBack: (source?: string) => boolean;
21
23
  getState: () => State;
22
24
  emitter: NavigationEventEmitter<any>;
23
25
  router: Router<State, Action>;
24
- stateRef: React.RefObject<State | null>;
25
26
  };
26
27
 
27
28
  /**
@@ -36,22 +37,26 @@ export function useNavigationHelpers<
36
37
  >({
37
38
  onAction,
38
39
  onUnhandledAction,
40
+ canGoBack,
39
41
  getState,
40
42
  emitter,
41
43
  router,
42
- stateRef,
43
44
  }: Options<State, Action>) {
44
- const parentNavigationHelpers = React.useContext(NavigationContext);
45
+ const parentNavigationHelpers = React.use(NavigationContext);
46
+
47
+ const { withStackTrace } = useNavigationBuilderContext();
45
48
 
46
49
  return React.useMemo(() => {
47
50
  const dispatch = (op: Action | ((state: State) => Action)) => {
48
- const action = typeof op === 'function' ? op(getState()) : op;
51
+ withStackTrace(dispatch, () => {
52
+ const action = typeof op === 'function' ? op(getState()) : op;
49
53
 
50
- const handled = onAction(action);
54
+ const handled = onAction(action);
51
55
 
52
- if (!handled) {
53
- onUnhandledAction?.(action);
54
- }
56
+ if (!handled) {
57
+ onUnhandledAction?.(action);
58
+ }
59
+ });
55
60
  };
56
61
 
57
62
  const actions = {
@@ -60,8 +65,15 @@ export function useNavigationHelpers<
60
65
  };
61
66
 
62
67
  const helpers = Object.keys(actions).reduce((acc, name) => {
68
+ const helper = (...args: any) =>
69
+ withStackTrace(helper, () =>
70
+ // @ts-expect-error: name is a valid key, but TypeScript is dumb
71
+ dispatch(actions[name](...args))
72
+ );
73
+
63
74
  // @ts-expect-error: name is a valid key, but TypeScript is dumb
64
- acc[name] = (...args: any) => dispatch(actions[name](...args));
75
+ acc[name] = helper;
76
+
65
77
  return acc;
66
78
  }, {} as ActionHelpers);
67
79
 
@@ -73,41 +85,19 @@ export function useNavigationHelpers<
73
85
  isFocused: parentNavigationHelpers
74
86
  ? parentNavigationHelpers.isFocused
75
87
  : () => true,
76
- canGoBack: () => {
77
- const state = getState();
78
-
79
- return (
80
- router.getStateForAction(state, CommonActions.goBack() as Action, {
81
- routeNames: state.routeNames,
82
- routeParamList: {},
83
- routeGetIdList: {},
84
- }) !== null ||
85
- parentNavigationHelpers?.canGoBack() ||
86
- false
87
- );
88
- },
89
- getState: (): State => {
90
- // FIXME: Workaround for when the state is read during render
91
- // By this time, we haven't committed the new state yet
92
- // Without this `useSyncExternalStore` will keep reading the old state
93
- // This may result in `useNavigationState` or `useIsFocused` returning wrong values
94
- // Apart from `useSyncExternalStore`, `getState` should never be called during render
95
- if (stateRef.current != null) {
96
- return stateRef.current;
97
- }
98
-
99
- return getState();
100
- },
101
- } as NavigationHelpers<ParamListBase, EventMap> & ActionHelpers;
88
+ canGoBack: () => canGoBack(),
89
+ getState,
90
+ } as NavigationHelpers<ParamListBase, State, EventMap> & ActionHelpers;
102
91
 
103
92
  return navigationHelpers;
104
93
  }, [
105
- router,
94
+ router.actionCreators,
106
95
  parentNavigationHelpers,
107
96
  emitter.emit,
97
+ canGoBack,
108
98
  getState,
109
99
  onAction,
110
100
  onUnhandledAction,
111
- stateRef,
101
+ withStackTrace,
112
102
  ]);
113
103
  }
@@ -3,5 +3,5 @@ import * as React from 'react';
3
3
  import { NavigationIndependentTreeContext } from './NavigationIndependentTreeContext';
4
4
 
5
5
  export function useNavigationIndependentTree() {
6
- return React.useContext(NavigationIndependentTreeContext);
6
+ return React.use(NavigationIndependentTreeContext);
7
7
  }