react-router 6.0.0-beta.3 → 6.0.0-beta.7

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.
File without changes
package/README.md CHANGED
@@ -2,9 +2,9 @@
2
2
 
3
3
  The `react-router` package is the heart of [React Router](/) and provides all
4
4
  the core functionality for both
5
- [`react-router-dom`](https://github.com/ReactTraining/react-router/packages/react-router-dom)
5
+ [`react-router-dom`](/packages/react-router-dom)
6
6
  and
7
- [`react-router-native`](https://github.com/ReactTraining/react-router/packages/react-router-native).
7
+ [`react-router-native`](/packages/react-router-native).
8
8
 
9
9
  If you're using React Router, you should never `import` anything directly from
10
10
  the `react-router` package, but you should have everything you need in either
package/index.d.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  import * as React from "react";
2
2
  import { Action } from "history";
3
- import type { Blocker, History, InitialEntry, Location, Path, State, To } from "history";
3
+ import type { History, InitialEntry, Location, Path, State, To } from "history";
4
4
  /**
5
5
  * A Navigator is a "location changer"; it's how you get to different locations.
6
6
  *
@@ -10,23 +10,25 @@ import type { Blocker, History, InitialEntry, Location, Path, State, To } from "
10
10
  * to avoid "tearing" that may occur in a suspense-enabled app if the action
11
11
  * and/or location were to be read directly from the history instance.
12
12
  */
13
- export declare type Navigator = Omit<History, "action" | "location" | "back" | "forward" | "listen">;
14
- declare const NavigatorContext: React.Context<Navigator>;
15
- declare const LocationContext: React.Context<LocationContextObject>;
16
- interface LocationContextObject {
17
- action?: Action;
18
- location?: Location;
13
+ export declare type Navigator = Omit<History, "action" | "location" | "back" | "forward" | "listen" | "block">;
14
+ interface NavigationContextObject {
15
+ basename: string;
16
+ navigator: Navigator;
19
17
  static: boolean;
20
18
  }
21
- declare const RouteContext: React.Context<RouteContextObject>;
19
+ declare const NavigationContext: React.Context<NavigationContextObject>;
20
+ interface LocationContextObject {
21
+ action: Action;
22
+ location: Location;
23
+ }
24
+ declare const LocationContext: React.Context<LocationContextObject>;
22
25
  interface RouteContextObject {
23
26
  outlet: React.ReactElement | null;
24
- params: Params;
25
- pathname: string;
26
- basename: string;
27
- route: RouteObject | null;
27
+ matches: RouteMatch[];
28
28
  }
29
+ declare const RouteContext: React.Context<RouteContextObject>;
29
30
  export interface MemoryRouterProps {
31
+ basename?: string;
30
32
  children?: React.ReactNode;
31
33
  initialEntries?: InitialEntry[];
32
34
  initialIndex?: number;
@@ -36,7 +38,7 @@ export interface MemoryRouterProps {
36
38
  *
37
39
  * @see https://reactrouter.com/api/MemoryRouter
38
40
  */
39
- export declare function MemoryRouter({ children, initialEntries, initialIndex }: MemoryRouterProps): React.ReactElement;
41
+ export declare function MemoryRouter({ basename, children, initialEntries, initialIndex }: MemoryRouterProps): React.ReactElement;
40
42
  export interface NavigateProps {
41
43
  to: To;
42
44
  replace?: boolean;
@@ -64,18 +66,35 @@ export interface RouteProps {
64
66
  caseSensitive?: boolean;
65
67
  children?: React.ReactNode;
66
68
  element?: React.ReactElement | null;
69
+ index?: boolean;
67
70
  path?: string;
68
71
  }
72
+ export interface PathRouteProps {
73
+ caseSensitive?: boolean;
74
+ children?: React.ReactNode;
75
+ element?: React.ReactElement | null;
76
+ index?: false;
77
+ path: string;
78
+ }
79
+ export interface LayoutRouteProps {
80
+ children?: React.ReactNode;
81
+ element?: React.ReactElement | null;
82
+ }
83
+ export interface IndexRouteProps {
84
+ element?: React.ReactElement | null;
85
+ index: true;
86
+ }
69
87
  /**
70
88
  * Declares an element that should be rendered at a certain URL path.
71
89
  *
72
90
  * @see https://reactrouter.com/api/Route
73
91
  */
74
- export declare function Route({ element }: RouteProps): React.ReactElement | null;
92
+ export declare function Route(_props: PathRouteProps | LayoutRouteProps | IndexRouteProps): React.ReactElement | null;
75
93
  export interface RouterProps {
76
94
  action?: Action;
95
+ basename?: string;
77
96
  children?: React.ReactNode;
78
- location: Location;
97
+ location: Partial<Location> | string;
79
98
  navigator: Navigator;
80
99
  static?: boolean;
81
100
  }
@@ -88,14 +107,10 @@ export interface RouterProps {
88
107
  *
89
108
  * @see https://reactrouter.com/api/Router
90
109
  */
91
- export declare function Router({ children, action, location, navigator, static: staticProp }: RouterProps): React.ReactElement;
92
- interface PartialLocation<S extends State = State> extends Omit<Partial<Location<S>>, "pathname"> {
93
- pathname: string;
94
- }
110
+ export declare function Router({ action, basename: basenameProp, children, location: locationProp, navigator, static: staticProp }: RouterProps): React.ReactElement | null;
95
111
  export interface RoutesProps {
96
- basename?: string;
97
112
  children?: React.ReactNode;
98
- location?: PartialLocation;
113
+ location?: Partial<Location> | string;
99
114
  }
100
115
  /**
101
116
  * A container for a nested tree of <Route> elements that renders the branch
@@ -103,14 +118,7 @@ export interface RoutesProps {
103
118
  *
104
119
  * @see https://reactrouter.com/api/Routes
105
120
  */
106
- export declare function Routes({ basename, children, location }: RoutesProps): React.ReactElement | null;
107
- /**
108
- * Blocks all navigation attempts. This is useful for preventing the page from
109
- * changing until some condition is met, like saving form data.
110
- *
111
- * @see https://reactrouter.com/api/useBlocker
112
- */
113
- export declare function useBlocker(blocker: Blocker, when?: boolean): void;
121
+ export declare function Routes({ children, location }: RoutesProps): React.ReactElement | null;
114
122
  /**
115
123
  * Returns the full href for the given "to" value. This is useful for building
116
124
  * custom links that are also accessible and preserve right-click behavior.
@@ -142,12 +150,7 @@ export declare function useLocation(): Location;
142
150
  *
143
151
  * @see https://reactrouter.com/api/useMatch
144
152
  */
145
- export declare function useMatch(pattern: PathPattern): PathMatch | null;
146
- declare type PathPattern = string | {
147
- path: string;
148
- caseSensitive?: boolean;
149
- end?: boolean;
150
- };
153
+ export declare function useMatch<ParamKey extends string = string>(pattern: PathPattern | string): PathMatch<ParamKey> | null;
151
154
  /**
152
155
  * The interface for the navigate() function returned from useNavigate().
153
156
  */
@@ -179,7 +182,7 @@ export declare function useOutlet(): React.ReactElement | null;
179
182
  *
180
183
  * @see https://reactrouter.com/api/useParams
181
184
  */
182
- export declare function useParams(): Params;
185
+ export declare function useParams<Key extends string = string>(): Readonly<Params<Key>>;
183
186
  /**
184
187
  * Resolves the pathname of the given `to` value against the current location.
185
188
  *
@@ -194,17 +197,7 @@ export declare function useResolvedPath(to: To): Path;
194
197
  *
195
198
  * @see https://reactrouter.com/api/useRoutes
196
199
  */
197
- export declare function useRoutes(partialRoutes: PartialRouteObject[], { basename, location }?: {
198
- basename?: string;
199
- location?: PartialLocation;
200
- }): React.ReactElement | null;
201
- /**
202
- * Creates a route config from an array of JavaScript objects. Used internally
203
- * by `useRoutes` to normalize the route config.
204
- *
205
- * @see https://reactrouter.com/api/createRoutesFromArray
206
- */
207
- export declare function createRoutesFromArray(array: PartialRouteObject[]): RouteObject[];
200
+ export declare function useRoutes(routes: RouteObject[], locationArg?: Partial<Location> | string): React.ReactElement | null;
208
201
  /**
209
202
  * Creates a route config from a React "children" object, which is usually
210
203
  * either a `<Route>` element or an array of them. Used internally by
@@ -216,26 +209,18 @@ export declare function createRoutesFromChildren(children: React.ReactNode): Rou
216
209
  /**
217
210
  * The parameters that were parsed from the URL path.
218
211
  */
219
- export declare type Params = Record<string, string>;
212
+ export declare type Params<Key extends string = string> = {
213
+ readonly [key in Key]: string | undefined;
214
+ };
220
215
  /**
221
216
  * A route object represents a logical route, with (optionally) its child
222
217
  * routes organized in a tree-like structure.
223
218
  */
224
219
  export interface RouteObject {
225
- caseSensitive: boolean;
226
- children?: RouteObject[];
227
- element: React.ReactNode;
228
- path: string;
229
- }
230
- /**
231
- * A "partial route" object is usually supplied by the user and may omit
232
- * certain properties of a real route object such as `path` and `element`,
233
- * which have reasonable defaults.
234
- */
235
- export interface PartialRouteObject {
236
220
  caseSensitive?: boolean;
237
- children?: PartialRouteObject[];
221
+ children?: RouteObject[];
238
222
  element?: React.ReactNode;
223
+ index?: boolean;
239
224
  path?: string;
240
225
  }
241
226
  /**
@@ -244,16 +229,77 @@ export interface PartialRouteObject {
244
229
  * @see https://reactrouter.com/api/generatePath
245
230
  */
246
231
  export declare function generatePath(path: string, params?: Params): string;
232
+ /**
233
+ * A RouteMatch contains info about how a route matched a URL.
234
+ */
235
+ export interface RouteMatch<ParamKey extends string = string> {
236
+ /**
237
+ * The names and values of dynamic parameters in the URL.
238
+ */
239
+ params: Params<ParamKey>;
240
+ /**
241
+ * The portion of the URL pathname that was matched.
242
+ */
243
+ pathname: string;
244
+ /**
245
+ * The portion of the URL pathname that was matched before child routes.
246
+ */
247
+ pathnameBase: string;
248
+ /**
249
+ * The route object that was used to match.
250
+ */
251
+ route: RouteObject;
252
+ }
247
253
  /**
248
254
  * Matches the given routes to a location and returns the match data.
249
255
  *
250
256
  * @see https://reactrouter.com/api/matchRoutes
251
257
  */
252
- export declare function matchRoutes(routes: PartialRouteObject[], location: string | Partial<Location>, basename?: string): RouteMatch[] | null;
253
- export interface RouteMatch {
254
- route: RouteObject;
258
+ export declare function matchRoutes(routes: RouteObject[], locationArg: Partial<Location> | string, basename?: string): RouteMatch[] | null;
259
+ /**
260
+ * Renders the result of `matchRoutes()` into a React element.
261
+ */
262
+ export declare function renderMatches(matches: RouteMatch[] | null): React.ReactElement | null;
263
+ /**
264
+ * A PathPattern is used to match on some portion of a URL pathname.
265
+ */
266
+ export interface PathPattern {
267
+ /**
268
+ * A string to match against a URL pathname. May contain `:id`-style segments
269
+ * to indicate placeholders for dynamic parameters. May also end with `/*` to
270
+ * indicate matching the rest of the URL pathname.
271
+ */
272
+ path: string;
273
+ /**
274
+ * Should be `true` if the static portions of the `path` should be matched in
275
+ * the same case.
276
+ */
277
+ caseSensitive?: boolean;
278
+ /**
279
+ * Should be `true` if this pattern should match the entire URL pathname.
280
+ */
281
+ end?: boolean;
282
+ }
283
+ /**
284
+ * A PathMatch contains info about how a PathPattern matched on a URL pathname.
285
+ */
286
+ export interface PathMatch<ParamKey extends string = string> {
287
+ /**
288
+ * The names and values of dynamic parameters in the URL.
289
+ */
290
+ params: Params<ParamKey>;
291
+ /**
292
+ * The portion of the URL pathname that was matched.
293
+ */
255
294
  pathname: string;
256
- params: Params;
295
+ /**
296
+ * The portion of the URL pathname that was matched before child routes.
297
+ */
298
+ pathnameBase: string;
299
+ /**
300
+ * The pattern that was used to match.
301
+ */
302
+ pattern: PathPattern;
257
303
  }
258
304
  /**
259
305
  * Performs pattern matching on a URL pathname and returns information about
@@ -261,16 +307,12 @@ export interface RouteMatch {
261
307
  *
262
308
  * @see https://reactrouter.com/api/matchPath
263
309
  */
264
- export declare function matchPath(pattern: PathPattern, pathname: string): PathMatch | null;
265
- export interface PathMatch {
266
- path: string;
267
- pathname: string;
268
- params: Params;
269
- }
310
+ export declare function matchPath<ParamKey extends string = string>(pattern: PathPattern | string, pathname: string): PathMatch<ParamKey> | null;
270
311
  /**
271
312
  * Returns a resolved path object relative to the given pathname.
272
313
  *
273
314
  * @see https://reactrouter.com/api/resolvePath
274
315
  */
275
- export declare function resolvePath(to: To, fromPathname?: string, basename?: string): Path;
276
- export {};
316
+ export declare function resolvePath(to: To, fromPathname?: string): Path;
317
+ /** @internal */
318
+ export { NavigationContext as UNSAFE_NavigationContext, LocationContext as UNSAFE_LocationContext, RouteContext as UNSAFE_RouteContext };