react-router 6.0.2 → 6.2.1

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.
package/index.d.ts CHANGED
@@ -11,7 +11,7 @@ export type { Location, Path, To, NavigationType };
11
11
  * to avoid "tearing" that may occur in a suspense-enabled app if the action
12
12
  * and/or location were to be read directly from the history instance.
13
13
  */
14
- export declare type Navigator = Omit<History, "action" | "location" | "back" | "forward" | "listen" | "block">;
14
+ export declare type Navigator = Pick<History, "go" | "push" | "replace" | "createHref">;
15
15
  interface NavigationContextObject {
16
16
  basename: string;
17
17
  navigator: Navigator;
@@ -56,33 +56,34 @@ export interface NavigateProps {
56
56
  */
57
57
  export declare function Navigate({ to, replace, state }: NavigateProps): null;
58
58
  export interface OutletProps {
59
+ context?: unknown;
59
60
  }
60
61
  /**
61
62
  * Renders the child route's element, if there is one.
62
63
  *
63
64
  * @see https://reactrouter.com/docs/en/v6/api#outlet
64
65
  */
65
- export declare function Outlet(_props: OutletProps): React.ReactElement | null;
66
+ export declare function Outlet(props: OutletProps): React.ReactElement | null;
66
67
  export interface RouteProps {
67
68
  caseSensitive?: boolean;
68
69
  children?: React.ReactNode;
69
- element?: React.ReactElement | null;
70
+ element?: React.ReactNode | null;
70
71
  index?: boolean;
71
72
  path?: string;
72
73
  }
73
74
  export interface PathRouteProps {
74
75
  caseSensitive?: boolean;
75
76
  children?: React.ReactNode;
76
- element?: React.ReactElement | null;
77
+ element?: React.ReactNode | null;
77
78
  index?: false;
78
79
  path: string;
79
80
  }
80
81
  export interface LayoutRouteProps {
81
82
  children?: React.ReactNode;
82
- element?: React.ReactElement | null;
83
+ element?: React.ReactNode | null;
83
84
  }
84
85
  export interface IndexRouteProps {
85
- element?: React.ReactElement | null;
86
+ element?: React.ReactNode | null;
86
87
  index: true;
87
88
  }
88
89
  /**
@@ -144,6 +145,11 @@ export declare function useInRouterContext(): boolean;
144
145
  * @see https://reactrouter.com/docs/en/v6/api#uselocation
145
146
  */
146
147
  export declare function useLocation(): Location;
148
+ declare type ParamParseFailed = {
149
+ failed: true;
150
+ };
151
+ declare type ParamParseSegment<Segment extends string> = Segment extends `${infer LeftSegment}/${infer RightSegment}` ? ParamParseSegment<LeftSegment> extends infer LeftResult ? ParamParseSegment<RightSegment> extends infer RightResult ? LeftResult extends string ? RightResult extends string ? LeftResult | RightResult : LeftResult : RightResult extends string ? RightResult : ParamParseFailed : ParamParseFailed : ParamParseSegment<RightSegment> extends infer RightResult ? RightResult extends string ? RightResult : ParamParseFailed : ParamParseFailed : Segment extends `:${infer Remaining}` ? Remaining : ParamParseFailed;
152
+ declare type ParamParseKey<Segment extends string> = ParamParseSegment<Segment> extends string ? ParamParseSegment<Segment> : string;
147
153
  /**
148
154
  * Returns the current navigation action which describes how the router came to
149
155
  * the current location, either by a pop, push, or replace on the history stack.
@@ -158,7 +164,7 @@ export declare function useNavigationType(): NavigationType;
158
164
  *
159
165
  * @see https://reactrouter.com/docs/en/v6/api#usematch
160
166
  */
161
- export declare function useMatch<ParamKey extends string = string>(pattern: PathPattern | string): PathMatch<ParamKey> | null;
167
+ export declare function useMatch<ParamKey extends ParamParseKey<Path>, Path extends string>(pattern: PathPattern<Path> | Path): PathMatch<ParamKey> | null;
162
168
  /**
163
169
  * The interface for the navigate() function returned from useNavigate().
164
170
  */
@@ -177,20 +183,28 @@ export interface NavigateOptions {
177
183
  * @see https://reactrouter.com/docs/en/v6/api#usenavigate
178
184
  */
179
185
  export declare function useNavigate(): NavigateFunction;
186
+ /**
187
+ * Returns the context (if provided) for the child route at this level of the route
188
+ * hierarchy.
189
+ * @see https://reactrouter.com/docs/en/v6/api#useoutletcontext
190
+ */
191
+ export declare function useOutletContext<Context = unknown>(): Context;
180
192
  /**
181
193
  * Returns the element for the child route at this level of the route
182
194
  * hierarchy. Used internally by <Outlet> to render child routes.
183
195
  *
184
196
  * @see https://reactrouter.com/docs/en/v6/api#useoutlet
185
197
  */
186
- export declare function useOutlet(): React.ReactElement | null;
198
+ export declare function useOutlet(context?: unknown): React.ReactElement | null;
187
199
  /**
188
200
  * Returns an object of key/value pairs of the dynamic params from the current
189
201
  * URL that were matched by the route path.
190
202
  *
191
203
  * @see https://reactrouter.com/docs/en/v6/api#useparams
192
204
  */
193
- export declare function useParams<Key extends string = string>(): Readonly<Params<Key>>;
205
+ export declare function useParams<ParamsOrKey extends string | Record<string, string | undefined> = string>(): Readonly<[
206
+ ParamsOrKey
207
+ ] extends [string] ? Params<ParamsOrKey> : Partial<ParamsOrKey>>;
194
208
  /**
195
209
  * Resolves the pathname of the given `to` value against the current location.
196
210
  *
@@ -271,13 +285,13 @@ export declare function renderMatches(matches: RouteMatch[] | null): React.React
271
285
  /**
272
286
  * A PathPattern is used to match on some portion of a URL pathname.
273
287
  */
274
- export interface PathPattern {
288
+ export interface PathPattern<Path extends string = string> {
275
289
  /**
276
290
  * A string to match against a URL pathname. May contain `:id`-style segments
277
291
  * to indicate placeholders for dynamic parameters. May also end with `/*` to
278
292
  * indicate matching the rest of the URL pathname.
279
293
  */
280
- path: string;
294
+ path: Path;
281
295
  /**
282
296
  * Should be `true` if the static portions of the `path` should be matched in
283
297
  * the same case.
@@ -315,7 +329,7 @@ export interface PathMatch<ParamKey extends string = string> {
315
329
  *
316
330
  * @see https://reactrouter.com/docs/en/v6/api#matchpath
317
331
  */
318
- export declare function matchPath<ParamKey extends string = string>(pattern: PathPattern | string, pathname: string): PathMatch<ParamKey> | null;
332
+ export declare function matchPath<ParamKey extends ParamParseKey<Path>, Path extends string>(pattern: PathPattern<Path> | Path, pathname: string): PathMatch<ParamKey> | null;
319
333
  /**
320
334
  * Returns a resolved path object relative to the given pathname.
321
335
  *
package/index.js CHANGED
@@ -1,5 +1,5 @@
1
1
  /**
2
- * React Router v6.0.2
2
+ * React Router v6.2.1
3
3
  *
4
4
  * Copyright (c) Remix Software Inc.
5
5
  *
@@ -147,8 +147,8 @@ function Navigate(_ref2) {
147
147
  *
148
148
  * @see https://reactrouter.com/docs/en/v6/api#outlet
149
149
  */
150
- function Outlet(_props) {
151
- return useOutlet();
150
+ function Outlet(props) {
151
+ return useOutlet(props.context);
152
152
  }
153
153
 
154
154
  /**
@@ -305,13 +305,13 @@ function useLocation() {
305
305
  "useLocation() may be used only in the context of a <Router> component.") : invariant(false) : void 0;
306
306
  return useContext(LocationContext).location;
307
307
  }
308
+
308
309
  /**
309
310
  * Returns the current navigation action which describes how the router came to
310
311
  * the current location, either by a pop, push, or replace on the history stack.
311
312
  *
312
313
  * @see https://reactrouter.com/docs/en/v6/api#usenavigationtype
313
314
  */
314
-
315
315
  function useNavigationType() {
316
316
  return useContext(LocationContext).navigationType;
317
317
  }
@@ -327,7 +327,10 @@ function useMatch(pattern) {
327
327
  !useInRouterContext() ? process.env.NODE_ENV !== "production" ? invariant(false, // TODO: This error is probably because they somehow have 2 versions of the
328
328
  // router loaded. We can help them understand how to avoid that.
329
329
  "useMatch() may be used only in the context of a <Router> component.") : invariant(false) : void 0;
330
- return matchPath(pattern, useLocation().pathname);
330
+ let {
331
+ pathname
332
+ } = useLocation();
333
+ return useMemo(() => matchPath(pattern, pathname), [pathname, pattern]);
331
334
  }
332
335
  /**
333
336
  * The interface for the navigate() function returned from useNavigate().
@@ -381,6 +384,16 @@ function useNavigate() {
381
384
  }, [basename, navigator, routePathnamesJson, locationPathname]);
382
385
  return navigate;
383
386
  }
387
+ const OutletContext = /*#__PURE__*/createContext(null);
388
+ /**
389
+ * Returns the context (if provided) for the child route at this level of the route
390
+ * hierarchy.
391
+ * @see https://reactrouter.com/docs/en/v6/api#useoutletcontext
392
+ */
393
+
394
+ function useOutletContext() {
395
+ return useContext(OutletContext);
396
+ }
384
397
  /**
385
398
  * Returns the element for the child route at this level of the route
386
399
  * hierarchy. Used internally by <Outlet> to render child routes.
@@ -388,8 +401,16 @@ function useNavigate() {
388
401
  * @see https://reactrouter.com/docs/en/v6/api#useoutlet
389
402
  */
390
403
 
391
- function useOutlet() {
392
- return useContext(RouteContext).outlet;
404
+ function useOutlet(context) {
405
+ let outlet = useContext(RouteContext).outlet;
406
+
407
+ if (outlet) {
408
+ return /*#__PURE__*/createElement(OutletContext.Provider, {
409
+ value: context
410
+ }, outlet);
411
+ }
412
+
413
+ return outlet;
393
414
  }
394
415
  /**
395
416
  * Returns an object of key/value pairs of the dynamic params from the current
@@ -465,7 +486,7 @@ function useRoutes(routes, locationArg) {
465
486
  // );
466
487
  // }
467
488
  let parentPath = parentRoute && parentRoute.path || "";
468
- warningOnce(parentPathname, !parentRoute || parentPath.endsWith("*"), "You rendered descendant <Routes> (or called `useRoutes()`) at " + ("\"" + parentPathname + "\" (under <Route path=\"" + parentPath + "\">) but the ") + "parent route path has no trailing \"*\". This means if you navigate " + "deeper, the parent won't match anymore and therefore the child " + "routes will never render.\n\n" + ("Please change the parent <Route path=\"" + parentPath + "\"> to <Route ") + ("path=\"" + parentPath + "/*\">."));
489
+ warningOnce(parentPathname, !parentRoute || parentPath.endsWith("*"), "You rendered descendant <Routes> (or called `useRoutes()`) at " + ("\"" + parentPathname + "\" (under <Route path=\"" + parentPath + "\">) but the ") + "parent route path has no trailing \"*\". This means if you navigate " + "deeper, the parent won't match anymore and therefore the child " + "routes will never render.\n\n" + ("Please change the parent <Route path=\"" + parentPath + "\"> to <Route ") + ("path=\"" + (parentPath === "/" ? "*" : parentPath + "/*") + "\">."));
469
490
  }
470
491
 
471
492
  let locationFromContext = useLocation();
@@ -585,7 +606,7 @@ function matchRoutes(routes, locationArg, basename) {
585
606
  let matches = null;
586
607
 
587
608
  for (let i = 0; matches == null && i < branches.length; ++i) {
588
- matches = matchRouteBranch(branches[i], routes, pathname);
609
+ matches = matchRouteBranch(branches[i], pathname);
589
610
  }
590
611
 
591
612
  return matches;
@@ -608,7 +629,8 @@ function flattenRoutes(routes, branches, parentsMeta, parentPath) {
608
629
  let meta = {
609
630
  relativePath: route.path || "",
610
631
  caseSensitive: route.caseSensitive === true,
611
- childrenIndex: index
632
+ childrenIndex: index,
633
+ route
612
634
  };
613
635
 
614
636
  if (meta.relativePath.startsWith("/")) {
@@ -681,9 +703,7 @@ function compareIndexes(a, b) {
681
703
  0;
682
704
  }
683
705
 
684
- function matchRouteBranch(branch, // TODO: attach original route object inside routesMeta so we don't need this arg
685
- routesArg, pathname) {
686
- let routes = routesArg;
706
+ function matchRouteBranch(branch, pathname) {
687
707
  let {
688
708
  routesMeta
689
709
  } = branch;
@@ -702,7 +722,7 @@ routesArg, pathname) {
702
722
  }, remainingPathname);
703
723
  if (!match) return null;
704
724
  Object.assign(matchedParams, match.params);
705
- let route = routes[meta.childrenIndex];
725
+ let route = meta.route;
706
726
  matches.push({
707
727
  params: matchedParams,
708
728
  pathname: joinPaths([matchedPathname, match.pathname]),
@@ -713,8 +733,6 @@ routesArg, pathname) {
713
733
  if (match.pathnameBase !== "/") {
714
734
  matchedPathname = joinPaths([matchedPathname, match.pathnameBase]);
715
735
  }
716
-
717
- routes = route.children;
718
736
  }
719
737
 
720
738
  return matches;
@@ -814,10 +832,10 @@ function compilePath(path, caseSensitive, end) {
814
832
  : "(?:\\/(.+)|\\/*)$"; // Don't include the / in params["*"]
815
833
  } else {
816
834
  regexpSource += end ? "\\/*$" // When matching to the end, ignore trailing slashes
817
- : // Otherwise, at least match a word boundary. This restricts parent
818
- // routes to matching only their own words and nothing more, e.g. parent
835
+ : // Otherwise, match a word boundary or a proceeding /. The word boundary restricts
836
+ // parent routes to matching only their own words and nothing more, e.g. parent
819
837
  // route "/home" should not match "/home2".
820
- "(?:\\b|$)";
838
+ "(?:\\b|\\/|$)";
821
839
  }
822
840
 
823
841
  let matcher = new RegExp(regexpSource, caseSensitive ? undefined : "i");
@@ -945,5 +963,5 @@ const normalizeSearch = search => !search || search === "?" ? "" : search.starts
945
963
 
946
964
  const normalizeHash = hash => !hash || hash === "#" ? "" : hash.startsWith("#") ? hash : "#" + hash; ///////////////////////////////////////////////////////////////////////////////
947
965
 
948
- export { MemoryRouter, Navigate, Outlet, Route, Router, Routes, LocationContext as UNSAFE_LocationContext, NavigationContext as UNSAFE_NavigationContext, RouteContext as UNSAFE_RouteContext, createRoutesFromChildren, generatePath, matchPath, matchRoutes, renderMatches, resolvePath, useHref, useInRouterContext, useLocation, useMatch, useNavigate, useNavigationType, useOutlet, useParams, useResolvedPath, useRoutes };
966
+ export { MemoryRouter, Navigate, Outlet, Route, Router, Routes, LocationContext as UNSAFE_LocationContext, NavigationContext as UNSAFE_NavigationContext, RouteContext as UNSAFE_RouteContext, createRoutesFromChildren, generatePath, matchPath, matchRoutes, renderMatches, resolvePath, useHref, useInRouterContext, useLocation, useMatch, useNavigate, useNavigationType, useOutlet, useOutletContext, useParams, useResolvedPath, useRoutes };
949
967
  //# sourceMappingURL=index.js.map