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

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 +173 -90
  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 +37 -12
  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 +29 -57
  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 +35 -27
  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 +3 -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 +4 -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 +459 -332
  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 +59 -21
  248. package/src/NavigationRootContext.tsx +11 -0
  249. package/src/NavigationStateContext.tsx +6 -6
  250. package/src/PreventRemoveContext.tsx +1 -0
  251. package/src/PreventRemoveProvider.tsx +55 -56
  252. package/src/SceneView.tsx +31 -57
  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 +44 -30
  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
@@ -0,0 +1,5 @@
1
+ import * as React from 'react';
2
+
3
+ export const ConsumedParamsContext = React.createContext<
4
+ WeakMap<object, true> | undefined
5
+ >(undefined);
@@ -0,0 +1,206 @@
1
+ import type { NavigationState, PartialState } from '@react-navigation/routers';
2
+
3
+ import { getStateFromRouteParams } from './getStateFromRouteParams';
4
+ import type { TreeForPathConfig } from './StaticNavigation';
5
+
6
+ function findScreenInConfig(config: TreeForPathConfig['config'], name: string) {
7
+ const screens = config.screens;
8
+
9
+ if (screens?.[name] != null) {
10
+ return screens[name];
11
+ }
12
+
13
+ if (config.groups) {
14
+ for (const group of Object.values(config.groups)) {
15
+ if (group.screens[name] != null) {
16
+ return group.screens[name];
17
+ }
18
+ }
19
+ }
20
+
21
+ return undefined;
22
+ }
23
+
24
+ function findInitialRouteName(
25
+ config: TreeForPathConfig['config']
26
+ ): string | undefined {
27
+ if (config.initialRouteName != null) {
28
+ return config.initialRouteName;
29
+ }
30
+
31
+ for (const key in config) {
32
+ if (key === 'screens' && config.screens) {
33
+ const name = Object.keys(config.screens)[0];
34
+
35
+ if (name != null) {
36
+ return name;
37
+ }
38
+ }
39
+
40
+ if (key === 'groups' && config.groups) {
41
+ for (const group of Object.values(config.groups)) {
42
+ const name = Object.keys(group.screens)[0];
43
+
44
+ if (name != null) {
45
+ return name;
46
+ }
47
+ }
48
+ }
49
+ }
50
+
51
+ return undefined;
52
+ }
53
+
54
+ /**
55
+ * Get loader for the focused route in a static config tree with given navigation state.
56
+ *
57
+ * @param tree The static navigation config.
58
+ * @param state The navigation state to extract the focused route path from.
59
+ * @returns A function that returns a `Promise<void>`, or `undefined` if no loaders are found.
60
+ *
61
+ * @example
62
+ * ```js
63
+ * const loader = getLoaderForState(RootStack, {
64
+ * index: 0,
65
+ * routes: [{ name: 'Home' }],
66
+ * });
67
+ * await loader?.();
68
+ * ```
69
+ */
70
+ export function getLoaderForState(
71
+ tree: TreeForPathConfig,
72
+ state: PartialState<NavigationState> | NavigationState | undefined
73
+ ): (() => Promise<void>) | undefined {
74
+ return getLoaderForStateChange(tree, state, undefined, undefined);
75
+ }
76
+
77
+ export function getLoaderForStateChange(
78
+ tree: TreeForPathConfig,
79
+ state: PartialState<NavigationState> | NavigationState | undefined,
80
+ previousState: PartialState<NavigationState> | NavigationState | undefined,
81
+ consumedParams: WeakMap<object, true> | undefined
82
+ ): (() => Promise<void>) | undefined {
83
+ const focusedRoute = state?.routes[state.index ?? state.routes.length - 1];
84
+ const previousFocusedRoute =
85
+ previousState?.routes[
86
+ previousState.index ?? previousState.routes.length - 1
87
+ ];
88
+
89
+ if (!focusedRoute) {
90
+ return undefined;
91
+ }
92
+
93
+ const isNewlyFocused =
94
+ previousState === undefined ||
95
+ previousFocusedRoute == null ||
96
+ focusedRoute.name !== previousFocusedRoute.name ||
97
+ (focusedRoute.key != null &&
98
+ previousFocusedRoute.key != null &&
99
+ focusedRoute.key !== previousFocusedRoute.key);
100
+
101
+ const item = findScreenInConfig(tree.config, focusedRoute.name);
102
+
103
+ if (item == null) {
104
+ return undefined;
105
+ }
106
+
107
+ const initialParams =
108
+ typeof item === 'object' && 'initialParams' in item
109
+ ? item.initialParams
110
+ : undefined;
111
+
112
+ const params =
113
+ initialParams != null || focusedRoute.params != null
114
+ ? { ...initialParams, ...focusedRoute.params }
115
+ : undefined;
116
+
117
+ const loaders: (() => Promise<void>)[] = [];
118
+
119
+ if (
120
+ isNewlyFocused &&
121
+ 'UNSTABLE_loader' in item &&
122
+ typeof item.UNSTABLE_loader === 'function'
123
+ ) {
124
+ const loader = item.UNSTABLE_loader;
125
+
126
+ loaders.push(() =>
127
+ loader({
128
+ name: focusedRoute.name,
129
+ params,
130
+ })
131
+ );
132
+ }
133
+
134
+ const nested =
135
+ 'config' in item
136
+ ? item
137
+ : 'screen' in item &&
138
+ // Nested navigators cannot be defined as a getter
139
+ Object.getOwnPropertyDescriptor(item, 'screen')?.get == null &&
140
+ 'config' in item.screen
141
+ ? item.screen
142
+ : undefined;
143
+
144
+ if (nested) {
145
+ const initialRouteName = findInitialRouteName(nested.config);
146
+
147
+ const stateFromParams =
148
+ focusedRoute.params != null && consumedParams?.has(focusedRoute.params)
149
+ ? undefined
150
+ : getStateFromRouteParams(params);
151
+
152
+ let childState =
153
+ previousState !== undefined && stateFromParams != null
154
+ ? stateFromParams
155
+ : (focusedRoute.state ?? stateFromParams);
156
+
157
+ if (childState == null && initialRouteName != null) {
158
+ childState = { routes: [{ name: initialRouteName }] };
159
+ }
160
+
161
+ let previousChildState:
162
+ | PartialState<NavigationState>
163
+ | NavigationState
164
+ | undefined;
165
+
166
+ if (!isNewlyFocused) {
167
+ const previousRouteParams = previousFocusedRoute?.params;
168
+ const previousParams =
169
+ initialParams != null || previousRouteParams != null
170
+ ? { ...initialParams, ...previousRouteParams }
171
+ : undefined;
172
+ const previousStateFromParams =
173
+ previousRouteParams != null && consumedParams?.has(previousRouteParams)
174
+ ? undefined
175
+ : getStateFromRouteParams(previousParams);
176
+
177
+ previousChildState =
178
+ previousFocusedRoute?.state ?? previousStateFromParams;
179
+
180
+ if (previousChildState == null && initialRouteName != null) {
181
+ previousChildState = { routes: [{ name: initialRouteName }] };
182
+ }
183
+ }
184
+
185
+ const childLoader = getLoaderForStateChange(
186
+ nested,
187
+ childState,
188
+ previousState === undefined
189
+ ? undefined
190
+ : (previousChildState ?? { routes: [] }),
191
+ consumedParams
192
+ );
193
+
194
+ if (childLoader) {
195
+ loaders.push(childLoader);
196
+ }
197
+ }
198
+
199
+ if (loaders.length === 0) {
200
+ return undefined;
201
+ }
202
+
203
+ return async () => {
204
+ await Promise.all(loaders.map((l) => l()));
205
+ };
206
+ }
@@ -2,6 +2,7 @@ import type {
2
2
  NavigationAction,
3
3
  NavigationState,
4
4
  ParamListBase,
5
+ PartialState,
5
6
  } from '@react-navigation/routers';
6
7
  import * as React from 'react';
7
8
 
@@ -14,6 +15,7 @@ export type ListenerMap = {
14
15
 
15
16
  export type KeyedListenerMap = {
16
17
  getState: GetStateListener;
18
+ getNavigation: GetNavigationListener;
17
19
  beforeRemove: ChildBeforeRemoveListener;
18
20
  };
19
21
 
@@ -46,19 +48,26 @@ export type FocusedNavigationListener = <T>(
46
48
 
47
49
  export type GetStateListener = () => NavigationState;
48
50
 
49
- export type ChildBeforeRemoveListener = (action: NavigationAction) => boolean;
51
+ export type GetNavigationListener = () => NavigationHelpers<ParamListBase>;
50
52
 
51
- /**
52
- * Context which holds the required helpers needed to build nested navigators.
53
- */
54
- export const NavigationBuilderContext = React.createContext<{
55
- onAction?: (
56
- action: NavigationAction,
57
- visitedNavigators?: Set<string>
58
- ) => boolean;
59
- addListener?: AddListener;
60
- addKeyedListener?: AddKeyedListener;
61
- onRouteFocus?: (key: string) => void;
53
+ export type WithStackTrace = (
54
+ entry: (...args: never[]) => void,
55
+ callback: () => void
56
+ ) => void;
57
+
58
+ export type ChildBeforeRemoveListener = (
59
+ action: NavigationAction,
60
+ nextState: NavigationState | PartialState<NavigationState> | undefined
61
+ ) => boolean;
62
+
63
+ type NavigationBuilderContextValue = {
64
+ canGoBack?: ((source?: string) => boolean) | undefined;
65
+ onAction?:
66
+ | ((action: NavigationAction, visitedNavigators?: Set<string>) => boolean)
67
+ | undefined;
68
+ addListener?: AddListener | undefined;
69
+ addKeyedListener?: AddKeyedListener | undefined;
70
+ onRouteFocus?: ((key: string) => void) | undefined;
62
71
  onDispatchAction: (action: NavigationAction, noop: boolean) => void;
63
72
  onEmitEvent: (event: {
64
73
  type: string;
@@ -66,18 +75,28 @@ export const NavigationBuilderContext = React.createContext<{
66
75
  target: string | undefined;
67
76
  data: unknown;
68
77
  }) => void;
69
- onOptionsChange: (options: object) => void;
78
+ onOptionsChange: () => void;
79
+ getIsStateEmitted: () => boolean;
70
80
  scheduleUpdate: (callback: () => void) => void;
71
81
  flushUpdates: () => void;
72
- stackRef?: React.MutableRefObject<string | undefined>;
73
- }>({
74
- onDispatchAction: () => undefined,
75
- onEmitEvent: () => undefined,
76
- onOptionsChange: () => undefined,
77
- scheduleUpdate: () => {
78
- throw new Error("Couldn't find a context for scheduling updates.");
79
- },
80
- flushUpdates: () => {
81
- throw new Error("Couldn't find a context for flushing updates.");
82
- },
83
- });
82
+ withStackTrace: WithStackTrace;
83
+ };
84
+
85
+ /**
86
+ * Context which holds the required helpers needed to build nested navigators.
87
+ */
88
+ export const NavigationBuilderContext = React.createContext<
89
+ NavigationBuilderContextValue | undefined
90
+ >(undefined);
91
+
92
+ export function useNavigationBuilderContext() {
93
+ const value = React.use(NavigationBuilderContext);
94
+
95
+ if (value == null) {
96
+ throw new Error(
97
+ "Couldn't find a navigation builder context. Is your component inside NavigationContainer?"
98
+ );
99
+ }
100
+
101
+ return value;
102
+ }
@@ -3,11 +3,11 @@ import * as React from 'react';
3
3
  export type FocusedRouteState = {
4
4
  routes: [
5
5
  {
6
- key?: string;
6
+ key?: string | undefined;
7
7
  name: string;
8
- params?: object;
9
- path?: string;
10
- state?: FocusedRouteState;
8
+ params?: object | undefined;
9
+ path?: string | undefined;
10
+ state?: FocusedRouteState | undefined;
11
11
  },
12
12
  ];
13
13
  };
@@ -1,12 +1,28 @@
1
1
  import * as React from 'react';
2
2
 
3
+ import { NavigationBuilderContext } from './NavigationBuilderContext';
4
+ import { NavigationContainerRefContext } from './NavigationContainerRefContext';
5
+ import { NavigationFocusedRouteStateContext } from './NavigationFocusedRouteStateContext';
6
+ import { NavigationHelpersContext } from './NavigationHelpersContext';
3
7
  import { NavigationIndependentTreeContext } from './NavigationIndependentTreeContext';
4
8
  import {
9
+ IsScreenContext,
5
10
  NamedRouteContextListContext,
6
11
  NavigationContext,
7
12
  NavigationRouteContext,
8
- NavigationRouteNameContext,
9
13
  } from './NavigationProvider';
14
+ import { NavigationRootContext } from './NavigationRootContext';
15
+ import { PreventRemoveContext } from './PreventRemoveContext';
16
+ import { StaticTreeContext } from './StaticTreeContext';
17
+ import {
18
+ FocusedRouteKeyContext,
19
+ IsFocusedContext,
20
+ IsFocusedGetterContext,
21
+ } from './useIsFocused';
22
+ import {
23
+ NamedNavigationStateListenerListContext,
24
+ NavigationStateListenerContext,
25
+ } from './useNavigationState';
10
26
 
11
27
  /**
12
28
  * Component to make the child navigation container independent of parent containers.
@@ -18,16 +34,48 @@ export function NavigationIndependentTree({
18
34
  }) {
19
35
  return (
20
36
  // We need to clear any existing contexts for nested independent container to work correctly
21
- <NamedRouteContextListContext.Provider value={undefined}>
22
- <NavigationRouteContext.Provider value={undefined}>
23
- <NavigationContext.Provider value={undefined}>
24
- <NavigationRouteNameContext.Provider value={undefined}>
25
- <NavigationIndependentTreeContext.Provider value={true}>
26
- {children}
27
- </NavigationIndependentTreeContext.Provider>
28
- </NavigationRouteNameContext.Provider>
29
- </NavigationContext.Provider>
30
- </NavigationRouteContext.Provider>
31
- </NamedRouteContextListContext.Provider>
37
+ <NavigationIndependentTreeContext.Provider value={true}>
38
+ <NavigationContainerRefContext.Provider value={undefined}>
39
+ <NavigationRootContext.Provider value={undefined}>
40
+ <NavigationBuilderContext.Provider value={undefined}>
41
+ <StaticTreeContext.Provider value={undefined}>
42
+ <NavigationHelpersContext.Provider value={undefined}>
43
+ <NavigationStateListenerContext.Provider value={undefined}>
44
+ <FocusedRouteKeyContext.Provider value={undefined}>
45
+ <PreventRemoveContext.Provider value={undefined}>
46
+ <NamedNavigationStateListenerListContext.Provider
47
+ value={undefined}
48
+ >
49
+ <NamedRouteContextListContext.Provider
50
+ value={undefined}
51
+ >
52
+ <NavigationRouteContext.Provider value={undefined}>
53
+ <NavigationContext.Provider value={undefined}>
54
+ <IsFocusedContext.Provider value={undefined}>
55
+ <IsFocusedGetterContext.Provider
56
+ value={undefined}
57
+ >
58
+ <IsScreenContext.Provider value={false}>
59
+ <NavigationFocusedRouteStateContext.Provider
60
+ value={undefined}
61
+ >
62
+ {children}
63
+ </NavigationFocusedRouteStateContext.Provider>
64
+ </IsScreenContext.Provider>
65
+ </IsFocusedGetterContext.Provider>
66
+ </IsFocusedContext.Provider>
67
+ </NavigationContext.Provider>
68
+ </NavigationRouteContext.Provider>
69
+ </NamedRouteContextListContext.Provider>
70
+ </NamedNavigationStateListenerListContext.Provider>
71
+ </PreventRemoveContext.Provider>
72
+ </FocusedRouteKeyContext.Provider>
73
+ </NavigationStateListenerContext.Provider>
74
+ </NavigationHelpersContext.Provider>
75
+ </StaticTreeContext.Provider>
76
+ </NavigationBuilderContext.Provider>
77
+ </NavigationRootContext.Provider>
78
+ </NavigationContainerRefContext.Provider>
79
+ </NavigationIndependentTreeContext.Provider>
32
80
  );
33
81
  }
@@ -1,8 +1,15 @@
1
1
  import type { ParamListBase, Route } from '@react-navigation/routers';
2
2
  import * as React from 'react';
3
+ import useLatestCallback from 'use-latest-callback';
3
4
 
4
5
  import type { NavigationProp } from './types';
6
+ import {
7
+ FocusedRouteKeyContext,
8
+ IsFocusedContext,
9
+ IsFocusedGetterContext,
10
+ } from './useIsFocused';
5
11
  import { useLazyValue } from './useLazyValue';
12
+ import { NamedNavigationStateListenerProvider } from './useNavigationState';
6
13
 
7
14
  /**
8
15
  * Context which holds the route prop for a screen.
@@ -18,15 +25,10 @@ export const NavigationContext = React.createContext<
18
25
  NavigationProp<ParamListBase> | undefined
19
26
  >(undefined);
20
27
 
21
- export const NavigationRouteNameContext = React.createContext<
22
- string | undefined
23
- >(undefined);
24
-
25
- type Props = {
26
- route: Route<string>;
27
- navigation: NavigationProp<ParamListBase>;
28
- children: React.ReactNode;
29
- };
28
+ /**
29
+ * Context which indicates whether the component is inside a screen.
30
+ */
31
+ export const IsScreenContext = React.createContext(false);
30
32
 
31
33
  /**
32
34
  * Component to provide the navigation and route contexts to its children.
@@ -35,6 +37,12 @@ export const NamedRouteContextListContext = React.createContext<
35
37
  Record<string, React.Context<Route<string>>> | undefined
36
38
  >(undefined);
37
39
 
40
+ type Props = {
41
+ route: Route<string>;
42
+ navigation: NavigationProp<ParamListBase>;
43
+ children: React.ReactNode;
44
+ };
45
+
38
46
  export function NavigationProvider({ route, navigation, children }: Props) {
39
47
  const NamedRouteContext = useLazyValue(() => React.createContext(route));
40
48
 
@@ -48,17 +56,47 @@ export function NavigationProvider({ route, navigation, children }: Props) {
48
56
  [NamedRouteContext, parents, route.name]
49
57
  );
50
58
 
51
- return (
52
- <NamedRouteContextListContext.Provider value={NamedRouteContextList}>
53
- <NamedRouteContext.Provider value={route}>
54
- <NavigationRouteContext.Provider value={route}>
55
- <NavigationContext.Provider value={navigation}>
56
- <NavigationRouteNameContext.Provider value={route.name}>
57
- {children}
58
- </NavigationRouteNameContext.Provider>
59
- </NavigationContext.Provider>
60
- </NavigationRouteContext.Provider>
61
- </NamedRouteContext.Provider>
62
- </NamedRouteContextListContext.Provider>
59
+ const parentIsFocused = React.use(IsFocusedContext);
60
+ const focusedRouteKey = React.use(FocusedRouteKeyContext);
61
+
62
+ // Mark route as focused only if:
63
+ // - It doesn't have a parent navigator
64
+ // - Parent navigator is focused
65
+ const isFocused =
66
+ parentIsFocused == null || parentIsFocused
67
+ ? focusedRouteKey === route.key
68
+ : false;
69
+
70
+ const getIsFocused = useLatestCallback(() => isFocused);
71
+
72
+ return React.useMemo(
73
+ () => (
74
+ <NamedNavigationStateListenerProvider name={route.name}>
75
+ <NamedRouteContextListContext.Provider value={NamedRouteContextList}>
76
+ <NamedRouteContext.Provider value={route}>
77
+ <NavigationRouteContext.Provider value={route}>
78
+ <NavigationContext.Provider value={navigation}>
79
+ <IsFocusedContext.Provider value={isFocused}>
80
+ <IsFocusedGetterContext.Provider value={getIsFocused}>
81
+ <IsScreenContext.Provider value={true}>
82
+ {children}
83
+ </IsScreenContext.Provider>
84
+ </IsFocusedGetterContext.Provider>
85
+ </IsFocusedContext.Provider>
86
+ </NavigationContext.Provider>
87
+ </NavigationRouteContext.Provider>
88
+ </NamedRouteContext.Provider>
89
+ </NamedRouteContextListContext.Provider>
90
+ </NamedNavigationStateListenerProvider>
91
+ ),
92
+ [
93
+ NamedRouteContext,
94
+ NamedRouteContextList,
95
+ children,
96
+ getIsFocused,
97
+ isFocused,
98
+ navigation,
99
+ route,
100
+ ]
63
101
  );
64
102
  }
@@ -0,0 +1,11 @@
1
+ import type { ParamListBase } from '@react-navigation/routers';
2
+ import * as React from 'react';
3
+
4
+ import type { GenericNavigation } from './types';
5
+
6
+ /**
7
+ * Context which holds the navigation object for components outside a screen.
8
+ */
9
+ export const NavigationRootContext = React.createContext<
10
+ GenericNavigation<ParamListBase> | undefined
11
+ >(undefined);
@@ -5,19 +5,16 @@ const MISSING_CONTEXT_ERROR =
5
5
  "Couldn't find a navigation context. Have you wrapped your app with 'NavigationContainer'? See https://reactnavigation.org/docs/getting-started for setup instructions.";
6
6
 
7
7
  export const NavigationStateContext = React.createContext<{
8
- isDefault?: true;
9
- state?: NavigationState | PartialState<NavigationState>;
8
+ isDefault?: true | undefined;
9
+ state?: NavigationState | PartialState<NavigationState> | undefined;
10
10
  getKey: () => string | undefined;
11
11
  setKey: (key: string) => void;
12
12
  getState: () => NavigationState | PartialState<NavigationState> | undefined;
13
13
  setState: (
14
14
  state: NavigationState | PartialState<NavigationState> | undefined
15
15
  ) => void;
16
+ subscribe: (callback: () => void) => () => void;
16
17
  getIsInitial: () => boolean;
17
- addOptionsGetter?: (
18
- key: string,
19
- getter: () => object | undefined | null
20
- ) => void;
21
18
  }>({
22
19
  isDefault: true,
23
20
 
@@ -33,6 +30,9 @@ export const NavigationStateContext = React.createContext<{
33
30
  get setState(): any {
34
31
  throw new Error(MISSING_CONTEXT_ERROR);
35
32
  },
33
+ get subscribe(): any {
34
+ throw new Error(MISSING_CONTEXT_ERROR);
35
+ },
36
36
  get getIsInitial(): any {
37
37
  throw new Error(MISSING_CONTEXT_ERROR);
38
38
  },
@@ -14,6 +14,7 @@ export const PreventRemoveContext = React.createContext<
14
14
  routeKey: string,
15
15
  preventRemove: boolean
16
16
  ) => void;
17
+ notifyPreventRemove: () => void;
17
18
  }
18
19
  | undefined
19
20
  >(undefined);