@react-navigation/core 7.7.0 → 7.8.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 (61) hide show
  1. package/lib/commonjs/BaseNavigationContainer.js +2 -1
  2. package/lib/commonjs/BaseNavigationContainer.js.map +1 -1
  3. package/lib/commonjs/NavigationFocusedRouteStateContext.js +14 -0
  4. package/lib/commonjs/NavigationFocusedRouteStateContext.js.map +1 -0
  5. package/lib/commonjs/PreventRemoveProvider.js +1 -1
  6. package/lib/commonjs/PreventRemoveProvider.js.map +1 -1
  7. package/lib/commonjs/SceneView.js +41 -12
  8. package/lib/commonjs/SceneView.js.map +1 -1
  9. package/lib/commonjs/index.js +8 -0
  10. package/lib/commonjs/index.js.map +1 -1
  11. package/lib/commonjs/useNavigationBuilder.js +1 -0
  12. package/lib/commonjs/useNavigationBuilder.js.map +1 -1
  13. package/lib/commonjs/useNavigationHelpers.js +1 -0
  14. package/lib/commonjs/useNavigationHelpers.js.map +1 -1
  15. package/lib/commonjs/useStateForPath.js +21 -0
  16. package/lib/commonjs/useStateForPath.js.map +1 -0
  17. package/lib/module/BaseNavigationContainer.js +2 -1
  18. package/lib/module/BaseNavigationContainer.js.map +1 -1
  19. package/lib/module/NavigationFocusedRouteStateContext.js +8 -0
  20. package/lib/module/NavigationFocusedRouteStateContext.js.map +1 -0
  21. package/lib/module/PreventRemoveProvider.js +1 -1
  22. package/lib/module/PreventRemoveProvider.js.map +1 -1
  23. package/lib/module/SceneView.js +41 -12
  24. package/lib/module/SceneView.js.map +1 -1
  25. package/lib/module/index.js +1 -0
  26. package/lib/module/index.js.map +1 -1
  27. package/lib/module/useNavigationBuilder.js +1 -0
  28. package/lib/module/useNavigationBuilder.js.map +1 -1
  29. package/lib/module/useNavigationHelpers.js +1 -0
  30. package/lib/module/useNavigationHelpers.js.map +1 -1
  31. package/lib/module/useStateForPath.js +16 -0
  32. package/lib/module/useStateForPath.js.map +1 -0
  33. package/lib/typescript/commonjs/src/BaseNavigationContainer.d.ts.map +1 -1
  34. package/lib/typescript/commonjs/src/NavigationFocusedRouteStateContext.d.ts +16 -0
  35. package/lib/typescript/commonjs/src/NavigationFocusedRouteStateContext.d.ts.map +1 -0
  36. package/lib/typescript/commonjs/src/SceneView.d.ts.map +1 -1
  37. package/lib/typescript/commonjs/src/index.d.ts +1 -0
  38. package/lib/typescript/commonjs/src/index.d.ts.map +1 -1
  39. package/lib/typescript/commonjs/src/useNavigationBuilder.d.ts.map +1 -1
  40. package/lib/typescript/commonjs/src/useNavigationHelpers.d.ts.map +1 -1
  41. package/lib/typescript/commonjs/src/useStateForPath.d.ts +8 -0
  42. package/lib/typescript/commonjs/src/useStateForPath.d.ts.map +1 -0
  43. package/lib/typescript/module/src/BaseNavigationContainer.d.ts.map +1 -1
  44. package/lib/typescript/module/src/NavigationFocusedRouteStateContext.d.ts +16 -0
  45. package/lib/typescript/module/src/NavigationFocusedRouteStateContext.d.ts.map +1 -0
  46. package/lib/typescript/module/src/SceneView.d.ts.map +1 -1
  47. package/lib/typescript/module/src/index.d.ts +1 -0
  48. package/lib/typescript/module/src/index.d.ts.map +1 -1
  49. package/lib/typescript/module/src/useNavigationBuilder.d.ts.map +1 -1
  50. package/lib/typescript/module/src/useNavigationHelpers.d.ts.map +1 -1
  51. package/lib/typescript/module/src/useStateForPath.d.ts +8 -0
  52. package/lib/typescript/module/src/useStateForPath.d.ts.map +1 -0
  53. package/package.json +4 -4
  54. package/src/BaseNavigationContainer.tsx +2 -1
  55. package/src/NavigationFocusedRouteStateContext.tsx +19 -0
  56. package/src/PreventRemoveProvider.tsx +1 -1
  57. package/src/SceneView.tsx +58 -14
  58. package/src/index.tsx +1 -0
  59. package/src/useNavigationBuilder.tsx +1 -0
  60. package/src/useNavigationHelpers.tsx +1 -0
  61. package/src/useStateForPath.tsx +15 -0
package/src/SceneView.tsx CHANGED
@@ -7,6 +7,10 @@ import type {
7
7
  import * as React from 'react';
8
8
 
9
9
  import { EnsureSingleNavigator } from './EnsureSingleNavigator';
10
+ import {
11
+ type FocusedRouteState,
12
+ NavigationFocusedRouteStateContext,
13
+ } from './NavigationFocusedRouteStateContext';
10
14
  import { NavigationStateContext } from './NavigationStateContext';
11
15
  import { StaticContainer } from './StaticContainer';
12
16
  import type { NavigationProp, RouteConfigComponent } from './types';
@@ -122,6 +126,44 @@ export function SceneView<
122
126
 
123
127
  const getIsInitial = React.useCallback(() => isInitialRef.current, []);
124
128
 
129
+ const parentFocusedRouteState = React.useContext(
130
+ NavigationFocusedRouteStateContext
131
+ );
132
+
133
+ const focusedRouteState = React.useMemo(() => {
134
+ const state: FocusedRouteState = {
135
+ routes: [
136
+ {
137
+ name: route.name,
138
+ params: route.params,
139
+ path: route.path,
140
+ },
141
+ ],
142
+ };
143
+
144
+ // Add our state to the innermost route of the parent state
145
+ const addState = (
146
+ parent: FocusedRouteState | undefined
147
+ ): FocusedRouteState => {
148
+ const parentRoute = parent?.routes[0];
149
+
150
+ if (parentRoute) {
151
+ return {
152
+ routes: [
153
+ {
154
+ ...parentRoute,
155
+ state: addState(parentRoute.state),
156
+ },
157
+ ],
158
+ };
159
+ }
160
+
161
+ return state;
162
+ };
163
+
164
+ return addState(parentFocusedRouteState);
165
+ }, [parentFocusedRouteState, route.name, route.params, route.path]);
166
+
125
167
  const context = React.useMemo(
126
168
  () => ({
127
169
  state: routeState,
@@ -149,20 +191,22 @@ export function SceneView<
149
191
 
150
192
  return (
151
193
  <NavigationStateContext.Provider value={context}>
152
- <EnsureSingleNavigator>
153
- <StaticContainer
154
- name={screen.name}
155
- render={ScreenComponent || screen.children}
156
- navigation={navigation}
157
- route={route}
158
- >
159
- {ScreenComponent !== undefined ? (
160
- <ScreenComponent navigation={navigation} route={route} />
161
- ) : screen.children !== undefined ? (
162
- screen.children({ navigation, route })
163
- ) : null}
164
- </StaticContainer>
165
- </EnsureSingleNavigator>
194
+ <NavigationFocusedRouteStateContext.Provider value={focusedRouteState}>
195
+ <EnsureSingleNavigator>
196
+ <StaticContainer
197
+ name={screen.name}
198
+ render={ScreenComponent || screen.children}
199
+ navigation={navigation}
200
+ route={route}
201
+ >
202
+ {ScreenComponent !== undefined ? (
203
+ <ScreenComponent navigation={navigation} route={route} />
204
+ ) : screen.children !== undefined ? (
205
+ screen.children({ navigation, route })
206
+ ) : null}
207
+ </StaticContainer>
208
+ </EnsureSingleNavigator>
209
+ </NavigationFocusedRouteStateContext.Provider>
166
210
  </NavigationStateContext.Provider>
167
211
  );
168
212
  }
package/src/index.tsx CHANGED
@@ -36,5 +36,6 @@ export { useNavigationState } from './useNavigationState';
36
36
  export { usePreventRemove } from './usePreventRemove';
37
37
  export { usePreventRemoveContext } from './usePreventRemoveContext';
38
38
  export { useRoute } from './useRoute';
39
+ export { useStateForPath } from './useStateForPath';
39
40
  export { validatePathConfig } from './validatePathConfig';
40
41
  export * from '@react-navigation/routers';
@@ -49,6 +49,7 @@ import { useRegisterNavigator } from './useRegisterNavigator';
49
49
  import { useScheduleUpdate } from './useScheduleUpdate';
50
50
 
51
51
  // This is to make TypeScript compiler happy
52
+ // eslint-disable-next-line @typescript-eslint/no-unused-expressions
52
53
  PrivateValueStore;
53
54
 
54
55
  type NavigatorRoute = {
@@ -13,6 +13,7 @@ import { UnhandledActionContext } from './UnhandledActionContext';
13
13
  import type { NavigationEventEmitter } from './useEventEmitter';
14
14
 
15
15
  // This is to make TypeScript compiler happy
16
+ // eslint-disable-next-line @typescript-eslint/no-unused-expressions
16
17
  PrivateValueStore;
17
18
 
18
19
  type Options<State extends NavigationState, Action extends NavigationAction> = {
@@ -0,0 +1,15 @@
1
+ import * as React from 'react';
2
+
3
+ import { NavigationFocusedRouteStateContext } from './NavigationFocusedRouteStateContext';
4
+
5
+ /**
6
+ * Hook to get a minimal state representation for the current route.
7
+ * The returned state can be used with `getPathFromState` to build a path.
8
+ *
9
+ * @returns Minimal state to build a path for the current route.
10
+ */
11
+ export function useStateForPath() {
12
+ const state = React.useContext(NavigationFocusedRouteStateContext);
13
+
14
+ return state;
15
+ }