@react-navigation/native 6.0.6 → 6.0.9

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 (39) hide show
  1. package/lib/commonjs/Link.js +6 -5
  2. package/lib/commonjs/Link.js.map +1 -1
  3. package/lib/commonjs/NavigationContainer.js +9 -8
  4. package/lib/commonjs/NavigationContainer.js.map +1 -1
  5. package/lib/commonjs/ServerContainer.js +5 -4
  6. package/lib/commonjs/ServerContainer.js.map +1 -1
  7. package/lib/commonjs/ServerContext.js.map +1 -1
  8. package/lib/commonjs/index.js +16 -16
  9. package/lib/commonjs/theming/ThemeProvider.js +5 -4
  10. package/lib/commonjs/theming/ThemeProvider.js.map +1 -1
  11. package/lib/commonjs/useDocumentTitle.js +9 -8
  12. package/lib/commonjs/useDocumentTitle.js.map +1 -1
  13. package/lib/commonjs/useLinkProps.js +8 -5
  14. package/lib/commonjs/useLinkProps.js.map +1 -1
  15. package/lib/commonjs/useLinking.js +61 -40
  16. package/lib/commonjs/useLinking.js.map +1 -1
  17. package/lib/commonjs/useLinking.native.js +34 -29
  18. package/lib/commonjs/useLinking.native.js.map +1 -1
  19. package/lib/module/Link.js +6 -5
  20. package/lib/module/Link.js.map +1 -1
  21. package/lib/module/NavigationContainer.js +9 -8
  22. package/lib/module/NavigationContainer.js.map +1 -1
  23. package/lib/module/ServerContainer.js +5 -4
  24. package/lib/module/ServerContainer.js.map +1 -1
  25. package/lib/module/ServerContext.js.map +1 -1
  26. package/lib/module/theming/ThemeProvider.js +5 -4
  27. package/lib/module/theming/ThemeProvider.js.map +1 -1
  28. package/lib/module/useDocumentTitle.js +8 -7
  29. package/lib/module/useDocumentTitle.js.map +1 -1
  30. package/lib/module/useLinkProps.js +8 -5
  31. package/lib/module/useLinkProps.js.map +1 -1
  32. package/lib/module/useLinking.js +58 -38
  33. package/lib/module/useLinking.js.map +1 -1
  34. package/lib/module/useLinking.native.js +33 -28
  35. package/lib/module/useLinking.native.js.map +1 -1
  36. package/package.json +7 -6
  37. package/src/ServerContext.tsx +3 -2
  38. package/src/useLinking.native.tsx +6 -1
  39. package/src/useLinking.tsx +47 -15
@@ -7,6 +7,7 @@ import {
7
7
  NavigationState,
8
8
  ParamListBase,
9
9
  } from '@react-navigation/core';
10
+ import isEqual from 'fast-deep-equal';
10
11
  import { nanoid } from 'nanoid/non-secure';
11
12
  import * as React from 'react';
12
13
 
@@ -106,6 +107,7 @@ const createMemoryHistory = () => {
106
107
  // - This is the first time any state modifications are done
107
108
  // So we need to push the entry as there's nothing to replace
108
109
  items = [{ path, state, id }];
110
+ index = 0;
109
111
  } else {
110
112
  items[index] = { path, state, id };
111
113
  }
@@ -121,20 +123,14 @@ const createMemoryHistory = () => {
121
123
  go(n: number) {
122
124
  interrupt();
123
125
 
124
- if (n > 0) {
125
- // We shouldn't go forward more than available index
126
- n = Math.min(n, items.length - 1);
127
- } else if (n < 0) {
128
- // We shouldn't go back more than the 0 index
129
- // Otherwise we'll exit the page
130
- n = index + n < 0 ? -index : n;
131
- }
132
-
133
126
  if (n === 0) {
134
127
  return;
135
128
  }
136
129
 
137
- index += n;
130
+ // We shouldn't go back more than the 0 index (otherwise we'll exit the page)
131
+ // Or forward more than the available index (or the app will crash)
132
+ index =
133
+ n < 0 ? Math.max(index - n, 0) : Math.min(index + n, items.length - 1);
138
134
 
139
135
  // When we call `history.go`, `popstate` will fire when there's history to go back to
140
136
  // So we need to somehow handle following cases:
@@ -461,7 +457,12 @@ export default function useLinking(
461
457
  // Ignore any errors from deep linking.
462
458
  // This could happen in case of malformed links, navigation object not being initialized etc.
463
459
  console.warn(
464
- `An error occurred when trying to handle the link '${path}': ${e.message}`
460
+ `An error occurred when trying to handle the link '${path}': ${
461
+ typeof e === 'object' && e != null && 'message' in e
462
+ ? // @ts-expect-error: we're already checking for this
463
+ e.message
464
+ : e
465
+ }`
465
466
  );
466
467
  }
467
468
  } else {
@@ -482,6 +483,34 @@ export default function useLinking(
482
483
  return;
483
484
  }
484
485
 
486
+ const getPathForRoute = (
487
+ route: ReturnType<typeof findFocusedRoute>,
488
+ state: NavigationState
489
+ ): string => {
490
+ // If the `route` object contains a `path`, use that path as long as `route.name` and `params` still match
491
+ // This makes sure that we preserve the original URL for wildcard routes
492
+ if (route?.path) {
493
+ const stateForPath = getStateFromPathRef.current(
494
+ route.path,
495
+ configRef.current
496
+ );
497
+
498
+ if (stateForPath) {
499
+ const focusedRoute = findFocusedRoute(stateForPath);
500
+
501
+ if (
502
+ focusedRoute &&
503
+ focusedRoute.name === route.name &&
504
+ isEqual(focusedRoute.params, route.params)
505
+ ) {
506
+ return route.path;
507
+ }
508
+ }
509
+ }
510
+
511
+ return getPathFromStateRef.current(state, configRef.current);
512
+ };
513
+
485
514
  if (ref.current) {
486
515
  // We need to record the current metadata on the first render if they aren't set
487
516
  // This will allow the initial state to be in the history entry
@@ -489,8 +518,7 @@ export default function useLinking(
489
518
 
490
519
  if (state) {
491
520
  const route = findFocusedRoute(state);
492
- const path =
493
- route?.path ?? getPathFromStateRef.current(state, configRef.current);
521
+ const path = getPathForRoute(route, state);
494
522
 
495
523
  if (previousStateRef.current === undefined) {
496
524
  previousStateRef.current = state;
@@ -510,10 +538,14 @@ export default function useLinking(
510
538
  const previousState = previousStateRef.current;
511
539
  const state = navigation.getRootState();
512
540
 
541
+ // root state may not available, for example when root navigators switch inside the container
542
+ if (!state) {
543
+ return;
544
+ }
545
+
513
546
  const pendingPath = pendingPopStatePathRef.current;
514
547
  const route = findFocusedRoute(state);
515
- const path =
516
- route?.path ?? getPathFromStateRef.current(state, configRef.current);
548
+ const path = getPathForRoute(route, state);
517
549
 
518
550
  previousStateRef.current = state;
519
551
  pendingPopStatePathRef.current = undefined;