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

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 (84) hide show
  1. package/lib/module/BaseNavigationContainer.js.map +1 -1
  2. package/lib/module/NavigationBuilderContext.js.map +1 -1
  3. package/lib/module/NavigationIndependentTree.js +8 -4
  4. package/lib/module/NavigationIndependentTree.js.map +1 -1
  5. package/lib/module/NavigationProvider.js +14 -3
  6. package/lib/module/NavigationProvider.js.map +1 -1
  7. package/lib/module/NavigationStateContext.js.map +1 -1
  8. package/lib/module/SceneView.js +6 -39
  9. package/lib/module/SceneView.js.map +1 -1
  10. package/lib/module/StaticNavigation.js.map +1 -1
  11. package/lib/module/getPathFromState.js +24 -2
  12. package/lib/module/getPathFromState.js.map +1 -1
  13. package/lib/module/getStateFromPath.js +157 -72
  14. package/lib/module/getStateFromPath.js.map +1 -1
  15. package/lib/module/getStateFromRouteParams.js +24 -0
  16. package/lib/module/getStateFromRouteParams.js.map +1 -0
  17. package/lib/module/index.js.map +1 -1
  18. package/lib/module/types.js.map +1 -1
  19. package/lib/module/useIsFocused.js +7 -12
  20. package/lib/module/useIsFocused.js.map +1 -1
  21. package/lib/module/useNavigationBuilder.js +71 -28
  22. package/lib/module/useNavigationBuilder.js.map +1 -1
  23. package/lib/module/useOnAction.js.map +1 -1
  24. package/lib/module/useOnRouteFocus.js.map +1 -1
  25. package/lib/typescript/src/NavigationBuilderContext.d.ts +5 -5
  26. package/lib/typescript/src/NavigationBuilderContext.d.ts.map +1 -1
  27. package/lib/typescript/src/NavigationFocusedRouteStateContext.d.ts +4 -4
  28. package/lib/typescript/src/NavigationFocusedRouteStateContext.d.ts.map +1 -1
  29. package/lib/typescript/src/NavigationIndependentTree.d.ts.map +1 -1
  30. package/lib/typescript/src/NavigationProvider.d.ts +4 -4
  31. package/lib/typescript/src/NavigationProvider.d.ts.map +1 -1
  32. package/lib/typescript/src/NavigationStateContext.d.ts +3 -3
  33. package/lib/typescript/src/NavigationStateContext.d.ts.map +1 -1
  34. package/lib/typescript/src/SceneView.d.ts.map +1 -1
  35. package/lib/typescript/src/StaticNavigation.d.ts +5 -5
  36. package/lib/typescript/src/StaticNavigation.d.ts.map +1 -1
  37. package/lib/typescript/src/findFocusedRoute.d.ts +3 -3
  38. package/lib/typescript/src/findFocusedRoute.d.ts.map +1 -1
  39. package/lib/typescript/src/getActionFromState.d.ts +3 -3
  40. package/lib/typescript/src/getActionFromState.d.ts.map +1 -1
  41. package/lib/typescript/src/getPathFromState.d.ts +2 -2
  42. package/lib/typescript/src/getPathFromState.d.ts.map +1 -1
  43. package/lib/typescript/src/getStateFromPath.d.ts +3 -3
  44. package/lib/typescript/src/getStateFromPath.d.ts.map +1 -1
  45. package/lib/typescript/src/getStateFromRouteParams.d.ts +3 -0
  46. package/lib/typescript/src/getStateFromRouteParams.d.ts.map +1 -0
  47. package/lib/typescript/src/index.d.ts +1 -0
  48. package/lib/typescript/src/index.d.ts.map +1 -1
  49. package/lib/typescript/src/types.d.ts +64 -64
  50. package/lib/typescript/src/types.d.ts.map +1 -1
  51. package/lib/typescript/src/useDescriptors.d.ts +2 -2
  52. package/lib/typescript/src/useIsFocused.d.ts +3 -0
  53. package/lib/typescript/src/useIsFocused.d.ts.map +1 -1
  54. package/lib/typescript/src/useNavigationBuilder.d.ts +17 -17
  55. package/lib/typescript/src/useNavigationBuilder.d.ts.map +1 -1
  56. package/lib/typescript/src/useNavigationHelpers.d.ts +15 -15
  57. package/lib/typescript/src/useOnAction.d.ts +6 -6
  58. package/lib/typescript/src/useOnAction.d.ts.map +1 -1
  59. package/lib/typescript/src/useOnRouteFocus.d.ts +6 -6
  60. package/lib/typescript/src/useOnRouteFocus.d.ts.map +1 -1
  61. package/lib/typescript/src/useRouteCache.d.ts +2 -2
  62. package/lib/typescript/src/utilities.d.ts +35 -3
  63. package/lib/typescript/src/utilities.d.ts.map +1 -1
  64. package/package.json +10 -8
  65. package/src/BaseNavigationContainer.tsx +1 -1
  66. package/src/NavigationBuilderContext.tsx +7 -8
  67. package/src/NavigationFocusedRouteStateContext.tsx +4 -4
  68. package/src/NavigationIndependentTree.tsx +8 -5
  69. package/src/NavigationProvider.tsx +17 -3
  70. package/src/NavigationStateContext.tsx +5 -6
  71. package/src/SceneView.tsx +6 -36
  72. package/src/StaticNavigation.tsx +13 -5
  73. package/src/findFocusedRoute.tsx +3 -3
  74. package/src/getActionFromState.tsx +7 -7
  75. package/src/getPathFromState.tsx +52 -8
  76. package/src/getStateFromPath.tsx +254 -96
  77. package/src/getStateFromRouteParams.tsx +60 -0
  78. package/src/index.tsx +1 -0
  79. package/src/types.tsx +164 -120
  80. package/src/useIsFocused.tsx +14 -21
  81. package/src/useNavigationBuilder.tsx +116 -41
  82. package/src/useOnAction.tsx +7 -7
  83. package/src/useOnRouteFocus.tsx +9 -11
  84. package/src/utilities.tsx +72 -4
package/src/types.tsx CHANGED
@@ -14,6 +14,7 @@ import type {
14
14
  FlatType,
15
15
  KeyOf,
16
16
  NotUndefined,
17
+ StandardSchemaV1,
17
18
  UnionToIntersection,
18
19
  } from './utilities';
19
20
 
@@ -82,58 +83,68 @@ export type DefaultNavigatorOptions<
82
83
  * Layout for the navigator.
83
84
  * Useful for wrapping with a component with access to navigator's state and options.
84
85
  */
85
- layout?: (props: {
86
- state: State;
87
- navigation: NavigationHelpers<ParamList>;
88
- descriptors: Record<
89
- string,
90
- Descriptor<
91
- ScreenOptions,
92
- NavigationProp<
93
- ParamList,
94
- keyof ParamList,
95
- State,
96
- ScreenOptions,
97
- EventMap
98
- >,
99
- RouteProp<ParamList>
100
- >
101
- >;
102
- children: React.ReactNode;
103
- }) => React.ReactElement;
86
+ layout?:
87
+ | ((props: {
88
+ state: State;
89
+ navigation: NavigationHelpers<ParamList>;
90
+ descriptors: Record<
91
+ string,
92
+ Descriptor<
93
+ ScreenOptions,
94
+ NavigationProp<
95
+ ParamList,
96
+ keyof ParamList,
97
+ State,
98
+ ScreenOptions,
99
+ EventMap
100
+ >,
101
+ RouteProp<ParamList>
102
+ >
103
+ >;
104
+ children: React.ReactNode;
105
+ }) => React.ReactElement)
106
+ | undefined;
104
107
 
105
108
  /**
106
109
  * Event listeners for all the screens in the navigator.
107
110
  */
108
111
  screenListeners?:
109
- | ScreenListeners<State, EventMap>
110
- | ((props: {
111
- route: RouteProp<ParamList>;
112
- navigation: Navigation;
113
- }) => ScreenListeners<State, EventMap>);
112
+ | (
113
+ | ScreenListeners<State, EventMap>
114
+ | ((props: {
115
+ route: RouteProp<ParamList>;
116
+ navigation: Navigation;
117
+ }) => ScreenListeners<State, EventMap>)
118
+ )
119
+ | undefined;
114
120
 
115
121
  /**
116
122
  * Default options for all screens under this navigator.
117
123
  */
118
124
  screenOptions?:
119
- | ScreenOptions
120
- | ((props: {
121
- route: RouteProp<ParamList>;
122
- navigation: Navigation;
123
- theme: Theme;
124
- }) => ScreenOptions);
125
+ | (
126
+ | ScreenOptions
127
+ | ((props: {
128
+ route: RouteProp<ParamList>;
129
+ navigation: Navigation;
130
+ theme: Theme;
131
+ }) => ScreenOptions)
132
+ )
133
+ | undefined;
125
134
 
126
135
  /**
127
136
  * Layout for all screens under this navigator.
128
137
  */
129
- screenLayout?: (
130
- props: ScreenLayoutArgs<
131
- ParamList,
132
- keyof ParamList,
133
- ScreenOptions,
134
- Navigation
135
- >
136
- ) => React.ReactElement;
138
+ screenLayout?:
139
+ | ((
140
+ props: ScreenLayoutArgs<
141
+ ParamList,
142
+ keyof ParamList,
143
+ ScreenOptions,
144
+ Navigation
145
+ >
146
+ ) => React.ReactElement)
147
+ | undefined;
137
148
 
138
149
  /**
139
150
  * A function returning overrides for the underlying router used by the navigator.
@@ -142,9 +153,11 @@ export type DefaultNavigatorOptions<
142
153
  *
143
154
  * This must be a pure function and cannot reference outside dynamic variables.
144
155
  */
145
- router?: <Action extends NavigationAction>(
146
- original: Router<State, Action>
147
- ) => Partial<Router<State, Action>>;
156
+ router?:
157
+ | (<Action extends NavigationAction>(
158
+ original: Router<State, Action>
159
+ ) => Partial<Router<State, Action>>)
160
+ | undefined;
148
161
 
149
162
  /**
150
163
  * What should happen when the available route names change.
@@ -165,12 +178,12 @@ export type DefaultNavigatorOptions<
165
178
  * - Direct navigation is only handled for `NAVIGATE` actions.
166
179
  * - Unhandled state is restored only if the current state becomes invalid, i.e. it doesn't contain any currently defined screens.
167
180
  */
168
- routeNamesChangeBehavior?: 'firstMatch' | 'lastUnhandled';
181
+ routeNamesChangeBehavior?: ('firstMatch' | 'lastUnhandled') | undefined;
169
182
  };
170
183
 
171
184
  export type EventMapBase = Record<
172
185
  string,
173
- { data?: any; canPreventDefault?: boolean }
186
+ { data?: any; canPreventDefault?: boolean | undefined }
174
187
  >;
175
188
 
176
189
  export type EventMapCore<State extends NavigationState> = {
@@ -192,7 +205,7 @@ export type EventArg<
192
205
  /**
193
206
  * Key of the route which received the event.
194
207
  */
195
- readonly target?: string;
208
+ readonly target?: string | undefined;
196
209
  } & (CanPreventDefault extends true
197
210
  ? {
198
211
  /**
@@ -206,7 +219,7 @@ export type EventArg<
206
219
  }
207
220
  : {}) &
208
221
  (undefined extends Data
209
- ? { readonly data?: Readonly<Data> }
222
+ ? { readonly data?: Readonly<Data> | undefined }
210
223
  : { readonly data: Readonly<Data> });
211
224
 
212
225
  export type EventListenerCallback<
@@ -314,12 +327,18 @@ type NavigationHelpersCommon<
314
327
  ? [
315
328
  screen: RouteName,
316
329
  params?: ParamList[RouteName],
317
- options?: { merge?: boolean; pop?: boolean },
330
+ options?: {
331
+ merge?: boolean | undefined;
332
+ pop?: boolean | undefined;
333
+ },
318
334
  ]
319
335
  : [
320
336
  screen: RouteName,
321
337
  params: ParamList[RouteName],
322
- options?: { merge?: boolean; pop?: boolean },
338
+ options?: {
339
+ merge?: boolean | undefined;
340
+ pop?: boolean | undefined;
341
+ },
323
342
  ]
324
343
  : never
325
344
  ): void;
@@ -338,9 +357,9 @@ type NavigationHelpersCommon<
338
357
  ? {
339
358
  name: RouteName;
340
359
  params: ParamList[RouteName];
341
- path?: string;
342
- merge?: boolean;
343
- pop?: boolean;
360
+ path?: string | undefined;
361
+ merge?: boolean | undefined;
362
+ pop?: boolean | undefined;
344
363
  }
345
364
  : never
346
365
  ): void;
@@ -449,23 +468,27 @@ export type NavigationContainerProps = {
449
468
  /**
450
469
  * Initial state object for the navigation tree.
451
470
  */
452
- initialState?: InitialState;
471
+ initialState?: InitialState | undefined;
453
472
  /**
454
473
  * Callback which is called with the latest navigation state when it changes.
455
474
  */
456
- onStateChange?: (state: Readonly<NavigationState> | undefined) => void;
475
+ onStateChange?:
476
+ | ((state: Readonly<NavigationState> | undefined) => void)
477
+ | undefined;
457
478
  /**
458
479
  * Callback which is called after the navigation tree mounts.
459
480
  */
460
- onReady?: () => void;
481
+ onReady?: (() => void) | undefined;
461
482
  /**
462
483
  * Callback which is called when an action is not handled.
463
484
  */
464
- onUnhandledAction?: (action: Readonly<NavigationAction>) => void;
485
+ onUnhandledAction?:
486
+ | ((action: Readonly<NavigationAction>) => void)
487
+ | undefined;
465
488
  /**
466
489
  * Theme object for the UI elements.
467
490
  */
468
- theme?: Theme;
491
+ theme?: Theme | undefined;
469
492
  /**
470
493
  * Children elements to render.
471
494
  */
@@ -579,7 +602,7 @@ export type ScreenLayoutArgs<
579
602
 
580
603
  export type Descriptor<
581
604
  ScreenOptions extends {},
582
- Navigation extends NavigationProp<any, any, any, any, any>,
605
+ Navigation extends NavigationProp<ParamListBase, any, any, any, any>,
583
606
  Route extends RouteProp<any, any>,
584
607
  > = {
585
608
  /**
@@ -632,16 +655,16 @@ export type RouteConfigComponent<
632
655
  * React component to render for this screen.
633
656
  */
634
657
  component: ScreenComponentType<ParamList, RouteName>;
635
- getComponent?: never;
636
- children?: never;
658
+ getComponent?: never | undefined;
659
+ children?: never | undefined;
637
660
  }
638
661
  | {
639
662
  /**
640
663
  * Lazily get a React component to render for this screen.
641
664
  */
642
665
  getComponent: () => ScreenComponentType<ParamList, RouteName>;
643
- component?: never;
644
- children?: never;
666
+ component?: never | undefined;
667
+ children?: never | undefined;
645
668
  }
646
669
  | {
647
670
  /**
@@ -651,8 +674,8 @@ export type RouteConfigComponent<
651
674
  route: RouteProp<ParamList, RouteName>;
652
675
  navigation: any;
653
676
  }) => React.ReactNode;
654
- component?: never;
655
- getComponent?: never;
677
+ component?: never | undefined;
678
+ getComponent?: never | undefined;
656
679
  };
657
680
 
658
681
  export type RouteConfigProps<
@@ -668,7 +691,7 @@ export type RouteConfigProps<
668
691
  * If the key changes, existing screens with this name will be removed or reset.
669
692
  * Useful when we have some common screens and have conditional rendering.
670
693
  */
671
- navigationKey?: string;
694
+ navigationKey?: string | undefined;
672
695
 
673
696
  /**
674
697
  * Route name of this screen.
@@ -679,31 +702,39 @@ export type RouteConfigProps<
679
702
  * Navigator options for this screen.
680
703
  */
681
704
  options?:
682
- | ScreenOptions
683
- | ((props: {
684
- route: RouteProp<ParamList, RouteName>;
685
- navigation: Navigation;
686
- theme: Theme;
687
- }) => ScreenOptions);
705
+ | (
706
+ | ScreenOptions
707
+ | ((props: {
708
+ route: RouteProp<ParamList, RouteName>;
709
+ navigation: Navigation;
710
+ theme: Theme;
711
+ }) => ScreenOptions)
712
+ )
713
+ | undefined;
688
714
 
689
715
  /**
690
716
  * Event listeners for this screen.
691
717
  */
692
718
  listeners?:
693
- | ScreenListeners<State, EventMap>
694
- | ((props: {
695
- route: RouteProp<ParamList, RouteName>;
696
- navigation: Navigation;
697
- }) => ScreenListeners<State, EventMap>);
719
+ | (
720
+ | ScreenListeners<State, EventMap>
721
+ | ((props: {
722
+ route: RouteProp<ParamList, RouteName>;
723
+ navigation: Navigation;
724
+ }) => ScreenListeners<State, EventMap>)
725
+ )
726
+ | undefined;
698
727
 
699
728
  /**
700
729
  * Layout for this screen.
701
730
  * Useful for wrapping the screen with custom containers.
702
731
  * e.g. for styling, error boundaries, suspense, etc.
703
732
  */
704
- layout?: (
705
- props: ScreenLayoutArgs<ParamList, RouteName, ScreenOptions, Navigation>
706
- ) => React.ReactElement;
733
+ layout?:
734
+ | ((
735
+ props: ScreenLayoutArgs<ParamList, RouteName, ScreenOptions, Navigation>
736
+ ) => React.ReactElement)
737
+ | undefined;
707
738
 
708
739
  /**
709
740
  * Function to return an unique ID for this screen.
@@ -711,16 +742,18 @@ export type RouteConfigProps<
711
742
  * For a given screen name, there will always be only one screen corresponding to an ID.
712
743
  * If `undefined` is returned, it acts same as no `getId` being specified.
713
744
  */
714
- getId?: ({
715
- params,
716
- }: {
717
- params: Readonly<ParamList[RouteName]>;
718
- }) => string | undefined;
745
+ getId?:
746
+ | (({
747
+ params,
748
+ }: {
749
+ params: Readonly<ParamList[RouteName]>;
750
+ }) => string | undefined)
751
+ | undefined;
719
752
 
720
753
  /**
721
754
  * Initial params object for the route.
722
755
  */
723
- initialParams?: Partial<ParamList[RouteName]>;
756
+ initialParams?: Partial<ParamList[RouteName]> | undefined;
724
757
  };
725
758
 
726
759
  export type RouteConfig<
@@ -749,35 +782,41 @@ export type RouteGroupConfig<
749
782
  * Optional key for the screens in this group.
750
783
  * If the key changes, all existing screens in this group will be removed or reset.
751
784
  */
752
- navigationKey?: string;
785
+ navigationKey?: string | undefined;
753
786
 
754
787
  /**
755
788
  * Navigator options for this screen.
756
789
  */
757
790
  screenOptions?:
758
- | ScreenOptions
759
- | ((props: {
760
- route: RouteProp<ParamList, keyof ParamList>;
761
- navigation: Navigation;
762
- theme: Theme;
763
- }) => ScreenOptions);
791
+ | (
792
+ | ScreenOptions
793
+ | ((props: {
794
+ route: RouteProp<ParamList, keyof ParamList>;
795
+ navigation: Navigation;
796
+ theme: Theme;
797
+ }) => ScreenOptions)
798
+ )
799
+ | undefined;
764
800
 
765
801
  /**
766
802
  * Layout for the screens inside the group.
767
803
  * This will override the `screenLayout` of parent group or navigator.
768
804
  */
769
805
  screenLayout?:
770
- | ((
771
- props: ScreenLayoutArgs<
772
- ParamList,
773
- keyof ParamList,
774
- ScreenOptions,
775
- Navigation
776
- >
777
- ) => React.ReactElement)
778
- | {
779
- // FIXME: TypeScript doesn't seem to infer `navigation` correctly without this
780
- };
806
+ | (
807
+ | ((
808
+ props: ScreenLayoutArgs<
809
+ ParamList,
810
+ keyof ParamList,
811
+ ScreenOptions,
812
+ Navigation
813
+ >
814
+ ) => React.ReactElement)
815
+ | {
816
+ // FIXME: TypeScript doesn't seem to infer `navigation` correctly without this
817
+ }
818
+ )
819
+ | undefined;
781
820
 
782
821
  /**
783
822
  * Children React Elements to extract the route configuration from.
@@ -1173,33 +1212,35 @@ export type NavigatorScreenParams<ParamList extends {}> =
1173
1212
  merge?: never;
1174
1213
  initial?: never;
1175
1214
  pop?: never;
1176
- path?: string;
1215
+ path?: string | undefined;
1177
1216
  state: PartialState<NavigationState> | NavigationState | undefined;
1178
1217
  }
1179
1218
  | {
1180
1219
  [RouteName in keyof ParamList]: undefined extends ParamList[RouteName]
1181
1220
  ? {
1182
1221
  screen: RouteName;
1183
- params?: ParamList[RouteName];
1184
- merge?: boolean;
1185
- initial?: boolean;
1186
- path?: string;
1187
- pop?: boolean;
1222
+ params?: ParamList[RouteName] | undefined;
1223
+ merge?: boolean | undefined;
1224
+ initial?: boolean | undefined;
1225
+ path?: string | undefined;
1226
+ pop?: boolean | undefined;
1188
1227
  state?: never;
1189
1228
  }
1190
1229
  : {
1191
1230
  screen: RouteName;
1192
1231
  params: ParamList[RouteName];
1193
- merge?: boolean;
1194
- initial?: boolean;
1195
- path?: string;
1196
- pop?: boolean;
1232
+ merge?: boolean | undefined;
1233
+ initial?: boolean | undefined;
1234
+ path?: string | undefined;
1235
+ pop?: boolean | undefined;
1197
1236
  state?: never;
1198
1237
  };
1199
1238
  }[keyof ParamList];
1200
1239
 
1201
1240
  type ParseConfig<Params> = {
1202
- [K in keyof Params]?: (value: string) => Params[K];
1241
+ [K in keyof Params]?:
1242
+ | ((value: string) => Params[K])
1243
+ | StandardSchemaV1<string | string[] | null | undefined, Params[K]>;
1203
1244
  };
1204
1245
 
1205
1246
  type StringifyConfig<Params> = {
@@ -1217,9 +1258,9 @@ type PathConfigAlias<Params> = {
1217
1258
  * By default, paths are relating to the path config on the parent screen.
1218
1259
  * If `exact` is set to `true`, the parent path configuration is not used.
1219
1260
  */
1220
- exact?: boolean;
1261
+ exact?: boolean | undefined;
1221
1262
  /**
1222
- * An object mapping the param name to a function which parses the param value.
1263
+ * An object mapping the param name to a parser function or a Standard Schema.
1223
1264
  *
1224
1265
  * @example
1225
1266
  * ```js
@@ -1229,7 +1270,7 @@ type PathConfigAlias<Params> = {
1229
1270
  * }
1230
1271
  * ```
1231
1272
  */
1232
- parse?: ParseConfig<Params>;
1273
+ parse?: ParseConfig<Params> | undefined;
1233
1274
  };
1234
1275
 
1235
1276
  export type PathConfig<Params> = FlatType<
@@ -1246,25 +1287,28 @@ export type PathConfig<Params> = FlatType<
1246
1287
  * }
1247
1288
  * ```
1248
1289
  */
1249
- stringify?: StringifyConfig<Params>;
1290
+ stringify?: StringifyConfig<Params> | undefined;
1250
1291
  /**
1251
1292
  * Additional path alias that will be matched to the same screen.
1252
1293
  */
1253
- alias?: (string | PathConfigAlias<Params>)[];
1294
+ alias?: (string | PathConfigAlias<Params>)[] | undefined;
1254
1295
  } & (NonNullable<Params> extends NavigatorScreenParams<infer ParamList>
1255
1296
  ? {
1256
1297
  /**
1257
1298
  * Path configuration for child screens.
1258
1299
  */
1259
- screens?: PathConfigMap<ParamList>;
1300
+ screens?: PathConfigMap<ParamList> | undefined;
1260
1301
  /**
1261
1302
  * Name of the initial route to use for the navigator when the path matches.
1262
1303
  */
1263
- initialRouteName?: keyof ParamList;
1304
+ initialRouteName?: keyof ParamList | undefined;
1264
1305
  }
1265
1306
  : {})
1266
1307
  >;
1267
1308
 
1268
1309
  export type PathConfigMap<ParamList extends {}> = {
1269
- [RouteName in keyof ParamList]?: string | PathConfig<ParamList[RouteName]>;
1310
+ [RouteName in keyof ParamList]?:
1311
+ | string
1312
+ | PathConfig<ParamList[RouteName]>
1313
+ | undefined;
1270
1314
  };
@@ -1,32 +1,25 @@
1
1
  import * as React from 'react';
2
2
 
3
- import { useNavigation } from './useNavigation';
3
+ export const FocusedRouteKeyContext = React.createContext<string | undefined>(
4
+ undefined
5
+ );
6
+
7
+ export const IsFocusedContext = React.createContext<boolean | undefined>(
8
+ undefined
9
+ );
4
10
 
5
11
  /**
6
12
  * Hook to get the current focus state of the screen. Returns a `true` if screen is focused, otherwise `false`.
7
13
  * This can be used if a component needs to render something based on the focus state.
8
14
  */
9
15
  export function useIsFocused(): boolean {
10
- const navigation = useNavigation();
11
-
12
- const subscribe = React.useCallback(
13
- (callback: () => void) => {
14
- const unsubscribeFocus = navigation.addListener('focus', callback);
15
- const unsubscribeBlur = navigation.addListener('blur', callback);
16
-
17
- return () => {
18
- unsubscribeFocus();
19
- unsubscribeBlur();
20
- };
21
- },
22
- [navigation]
23
- );
16
+ const isFocused = React.use(IsFocusedContext);
24
17
 
25
- const value = React.useSyncExternalStore(
26
- subscribe,
27
- navigation.isFocused,
28
- navigation.isFocused
29
- );
18
+ if (isFocused === undefined) {
19
+ throw new Error(
20
+ "Couldn't determine focus state. Is your component inside a screen in a navigator?"
21
+ );
22
+ }
30
23
 
31
- return value;
24
+ return isFocused;
32
25
  }