react-router 6.2.2 → 6.4.0-pre.10
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/CHANGELOG.md +134 -0
- package/LICENSE.md +1 -1
- package/dist/index.d.ts +15 -0
- package/dist/index.js +1269 -0
- package/dist/index.js.map +1 -0
- package/dist/lib/components.d.ts +161 -0
- package/dist/lib/context.d.ts +48 -0
- package/dist/lib/hooks.d.ts +163 -0
- package/dist/lib/use-sync-external-store-shim/index.d.ts +6 -0
- package/dist/lib/use-sync-external-store-shim/useSyncExternalStoreShimClient.d.ts +7 -0
- package/dist/lib/use-sync-external-store-shim/useSyncExternalStoreShimServer.d.ts +9 -0
- package/{main.js → dist/main.js} +1 -1
- package/dist/react-router.development.js +1239 -0
- package/dist/react-router.development.js.map +1 -0
- package/dist/react-router.production.min.js +12 -0
- package/dist/react-router.production.min.js.map +1 -0
- package/dist/umd/react-router.development.js +1379 -0
- package/dist/umd/react-router.development.js.map +1 -0
- package/dist/umd/react-router.production.min.js +12 -0
- package/dist/umd/react-router.production.min.js.map +1 -0
- package/package.json +27 -18
- package/index.d.ts +0 -341
- package/index.js +0 -971
- package/index.js.map +0 -1
- package/react-router.development.js +0 -925
- package/react-router.development.js.map +0 -1
- package/react-router.production.min.js +0 -12
- package/react-router.production.min.js.map +0 -1
- package/umd/react-router.development.js +0 -1020
- package/umd/react-router.development.js.map +0 -1
- package/umd/react-router.production.min.js +0 -12
- package/umd/react-router.production.min.js.map +0 -1
package/dist/index.js
ADDED
|
@@ -0,0 +1,1269 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* React Router v6.4.0-pre.10
|
|
3
|
+
*
|
|
4
|
+
* Copyright (c) Remix Software Inc.
|
|
5
|
+
*
|
|
6
|
+
* This source code is licensed under the MIT license found in the
|
|
7
|
+
* LICENSE.md file in the root directory of this source tree.
|
|
8
|
+
*
|
|
9
|
+
* @license MIT
|
|
10
|
+
*/
|
|
11
|
+
import { invariant, resolveTo, joinPaths, matchPath, warning, parsePath, matchRoutes, isRouteErrorResponse, createMemoryRouter, createMemoryHistory, Action, stripBasename, isDeferredError } from '@remix-run/router';
|
|
12
|
+
export { Action as NavigationType, createPath, deferred, generatePath, isDeferredError, isRouteErrorResponse, json, matchPath, matchRoutes, parsePath, redirect, resolvePath } from '@remix-run/router';
|
|
13
|
+
import * as React from 'react';
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Copyright (c) Facebook, Inc. and its affiliates.
|
|
17
|
+
*
|
|
18
|
+
* This source code is licensed under the MIT license found in the
|
|
19
|
+
* LICENSE file in the root directory of this source tree.
|
|
20
|
+
*/
|
|
21
|
+
/**
|
|
22
|
+
* inlined Object.is polyfill to avoid requiring consumers ship their own
|
|
23
|
+
* https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/is
|
|
24
|
+
*/
|
|
25
|
+
|
|
26
|
+
function isPolyfill(x, y) {
|
|
27
|
+
return x === y && (x !== 0 || 1 / x === 1 / y) || x !== x && y !== y // eslint-disable-line no-self-compare
|
|
28
|
+
;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
const is = typeof Object.is === "function" ? Object.is : isPolyfill; // Intentionally not using named imports because Rollup uses dynamic
|
|
32
|
+
// dispatch for CommonJS interop named imports.
|
|
33
|
+
|
|
34
|
+
const {
|
|
35
|
+
useState,
|
|
36
|
+
useEffect,
|
|
37
|
+
useLayoutEffect,
|
|
38
|
+
useDebugValue
|
|
39
|
+
} = React;
|
|
40
|
+
let didWarnOld18Alpha = false;
|
|
41
|
+
let didWarnUncachedGetSnapshot = false; // Disclaimer: This shim breaks many of the rules of React, and only works
|
|
42
|
+
// because of a very particular set of implementation details and assumptions
|
|
43
|
+
// -- change any one of them and it will break. The most important assumption
|
|
44
|
+
// is that updates are always synchronous, because concurrent rendering is
|
|
45
|
+
// only available in versions of React that also have a built-in
|
|
46
|
+
// useSyncExternalStore API. And we only use this shim when the built-in API
|
|
47
|
+
// does not exist.
|
|
48
|
+
//
|
|
49
|
+
// Do not assume that the clever hacks used by this hook also work in general.
|
|
50
|
+
// The point of this shim is to replace the need for hacks by other libraries.
|
|
51
|
+
|
|
52
|
+
function useSyncExternalStore$2(subscribe, getSnapshot, // Note: The shim does not use getServerSnapshot, because pre-18 versions of
|
|
53
|
+
// React do not expose a way to check if we're hydrating. So users of the shim
|
|
54
|
+
// will need to track that themselves and return the correct value
|
|
55
|
+
// from `getSnapshot`.
|
|
56
|
+
getServerSnapshot) {
|
|
57
|
+
if (process.env.NODE_ENV !== "production") {
|
|
58
|
+
if (!didWarnOld18Alpha) {
|
|
59
|
+
if ("startTransition" in React) {
|
|
60
|
+
didWarnOld18Alpha = true;
|
|
61
|
+
console.error("You are using an outdated, pre-release alpha of React 18 that " + "does not support useSyncExternalStore. The " + "use-sync-external-store shim will not work correctly. Upgrade " + "to a newer pre-release.");
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
} // Read the current snapshot from the store on every render. Again, this
|
|
65
|
+
// breaks the rules of React, and only works here because of specific
|
|
66
|
+
// implementation details, most importantly that updates are
|
|
67
|
+
// always synchronous.
|
|
68
|
+
|
|
69
|
+
|
|
70
|
+
const value = getSnapshot();
|
|
71
|
+
|
|
72
|
+
if (process.env.NODE_ENV !== "production") {
|
|
73
|
+
if (!didWarnUncachedGetSnapshot) {
|
|
74
|
+
const cachedValue = getSnapshot();
|
|
75
|
+
|
|
76
|
+
if (!is(value, cachedValue)) {
|
|
77
|
+
console.error("The result of getSnapshot should be cached to avoid an infinite loop");
|
|
78
|
+
didWarnUncachedGetSnapshot = true;
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
} // Because updates are synchronous, we don't queue them. Instead we force a
|
|
82
|
+
// re-render whenever the subscribed state changes by updating an some
|
|
83
|
+
// arbitrary useState hook. Then, during render, we call getSnapshot to read
|
|
84
|
+
// the current value.
|
|
85
|
+
//
|
|
86
|
+
// Because we don't actually use the state returned by the useState hook, we
|
|
87
|
+
// can save a bit of memory by storing other stuff in that slot.
|
|
88
|
+
//
|
|
89
|
+
// To implement the early bailout, we need to track some things on a mutable
|
|
90
|
+
// object. Usually, we would put that in a useRef hook, but we can stash it in
|
|
91
|
+
// our useState hook instead.
|
|
92
|
+
//
|
|
93
|
+
// To force a re-render, we call forceUpdate({inst}). That works because the
|
|
94
|
+
// new object always fails an equality check.
|
|
95
|
+
|
|
96
|
+
|
|
97
|
+
const [{
|
|
98
|
+
inst
|
|
99
|
+
}, forceUpdate] = useState({
|
|
100
|
+
inst: {
|
|
101
|
+
value,
|
|
102
|
+
getSnapshot
|
|
103
|
+
}
|
|
104
|
+
}); // Track the latest getSnapshot function with a ref. This needs to be updated
|
|
105
|
+
// in the layout phase so we can access it during the tearing check that
|
|
106
|
+
// happens on subscribe.
|
|
107
|
+
|
|
108
|
+
useLayoutEffect(() => {
|
|
109
|
+
inst.value = value;
|
|
110
|
+
inst.getSnapshot = getSnapshot; // Whenever getSnapshot or subscribe changes, we need to check in the
|
|
111
|
+
// commit phase if there was an interleaved mutation. In concurrent mode
|
|
112
|
+
// this can happen all the time, but even in synchronous mode, an earlier
|
|
113
|
+
// effect may have mutated the store.
|
|
114
|
+
|
|
115
|
+
if (checkIfSnapshotChanged(inst)) {
|
|
116
|
+
// Force a re-render.
|
|
117
|
+
forceUpdate({
|
|
118
|
+
inst
|
|
119
|
+
});
|
|
120
|
+
} // eslint-disable-next-line react-hooks/exhaustive-deps
|
|
121
|
+
|
|
122
|
+
}, [subscribe, value, getSnapshot]);
|
|
123
|
+
useEffect(() => {
|
|
124
|
+
// Check for changes right before subscribing. Subsequent changes will be
|
|
125
|
+
// detected in the subscription handler.
|
|
126
|
+
if (checkIfSnapshotChanged(inst)) {
|
|
127
|
+
// Force a re-render.
|
|
128
|
+
forceUpdate({
|
|
129
|
+
inst
|
|
130
|
+
});
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
const handleStoreChange = () => {
|
|
134
|
+
// TODO: Because there is no cross-renderer API for batching updates, it's
|
|
135
|
+
// up to the consumer of this library to wrap their subscription event
|
|
136
|
+
// with unstable_batchedUpdates. Should we try to detect when this isn't
|
|
137
|
+
// the case and print a warning in development?
|
|
138
|
+
// The store changed. Check if the snapshot changed since the last time we
|
|
139
|
+
// read from the store.
|
|
140
|
+
if (checkIfSnapshotChanged(inst)) {
|
|
141
|
+
// Force a re-render.
|
|
142
|
+
forceUpdate({
|
|
143
|
+
inst
|
|
144
|
+
});
|
|
145
|
+
}
|
|
146
|
+
}; // Subscribe to the store and return a clean-up function.
|
|
147
|
+
|
|
148
|
+
|
|
149
|
+
return subscribe(handleStoreChange); // eslint-disable-next-line react-hooks/exhaustive-deps
|
|
150
|
+
}, [subscribe]);
|
|
151
|
+
useDebugValue(value);
|
|
152
|
+
return value;
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
function checkIfSnapshotChanged(inst) {
|
|
156
|
+
const latestGetSnapshot = inst.getSnapshot;
|
|
157
|
+
const prevValue = inst.value;
|
|
158
|
+
|
|
159
|
+
try {
|
|
160
|
+
const nextValue = latestGetSnapshot();
|
|
161
|
+
return !is(prevValue, nextValue);
|
|
162
|
+
} catch (error) {
|
|
163
|
+
return true;
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
/**
|
|
168
|
+
* Copyright (c) Facebook, Inc. and its affiliates.
|
|
169
|
+
*
|
|
170
|
+
* This source code is licensed under the MIT license found in the
|
|
171
|
+
* LICENSE file in the root directory of this source tree.
|
|
172
|
+
*
|
|
173
|
+
* @flow
|
|
174
|
+
*/
|
|
175
|
+
function useSyncExternalStore$1(subscribe, getSnapshot, getServerSnapshot) {
|
|
176
|
+
// Note: The shim does not use getServerSnapshot, because pre-18 versions of
|
|
177
|
+
// React do not expose a way to check if we're hydrating. So users of the shim
|
|
178
|
+
// will need to track that themselves and return the correct value
|
|
179
|
+
// from `getSnapshot`.
|
|
180
|
+
return getSnapshot();
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
/**
|
|
184
|
+
* Inlined into the react-router repo since use-sync-external-store does not
|
|
185
|
+
* provide a UMD-compatible package, so we need this to be able to distribute
|
|
186
|
+
* UMD react-router bundles
|
|
187
|
+
*/
|
|
188
|
+
const canUseDOM = !!(typeof window !== "undefined" && typeof window.document !== "undefined" && typeof window.document.createElement !== "undefined");
|
|
189
|
+
const isServerEnvironment = !canUseDOM;
|
|
190
|
+
const shim = isServerEnvironment ? useSyncExternalStore$1 : useSyncExternalStore$2;
|
|
191
|
+
const useSyncExternalStore = "useSyncExternalStore" in React ? // @ts-expect-error
|
|
192
|
+
(module => module.useSyncExternalStore)(React) : shim;
|
|
193
|
+
|
|
194
|
+
// Contexts for data routers
|
|
195
|
+
const DataStaticRouterContext = /*#__PURE__*/React.createContext(null);
|
|
196
|
+
|
|
197
|
+
if (process.env.NODE_ENV !== "production") {
|
|
198
|
+
DataStaticRouterContext.displayName = "DataStaticRouterContext";
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
const DataRouterContext = /*#__PURE__*/React.createContext(null);
|
|
202
|
+
|
|
203
|
+
if (process.env.NODE_ENV !== "production") {
|
|
204
|
+
DataRouterContext.displayName = "DataRouter";
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
const DataRouterStateContext = /*#__PURE__*/React.createContext(null);
|
|
208
|
+
|
|
209
|
+
if (process.env.NODE_ENV !== "production") {
|
|
210
|
+
DataRouterStateContext.displayName = "DataRouterState";
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
const DeferredContext = /*#__PURE__*/React.createContext(null);
|
|
214
|
+
|
|
215
|
+
if (process.env.NODE_ENV !== "production") {
|
|
216
|
+
DeferredContext.displayName = "Deferred";
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
const NavigationContext = /*#__PURE__*/React.createContext(null);
|
|
220
|
+
|
|
221
|
+
if (process.env.NODE_ENV !== "production") {
|
|
222
|
+
NavigationContext.displayName = "Navigation";
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
const LocationContext = /*#__PURE__*/React.createContext(null);
|
|
226
|
+
|
|
227
|
+
if (process.env.NODE_ENV !== "production") {
|
|
228
|
+
LocationContext.displayName = "Location";
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
const RouteContext = /*#__PURE__*/React.createContext({
|
|
232
|
+
outlet: null,
|
|
233
|
+
matches: []
|
|
234
|
+
});
|
|
235
|
+
|
|
236
|
+
if (process.env.NODE_ENV !== "production") {
|
|
237
|
+
RouteContext.displayName = "Route";
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
const RouteErrorContext = /*#__PURE__*/React.createContext(null);
|
|
241
|
+
|
|
242
|
+
if (process.env.NODE_ENV !== "production") {
|
|
243
|
+
RouteErrorContext.displayName = "RouteError";
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
/**
|
|
247
|
+
* Returns the full href for the given "to" value. This is useful for building
|
|
248
|
+
* custom links that are also accessible and preserve right-click behavior.
|
|
249
|
+
*
|
|
250
|
+
* @see https://reactrouter.com/docs/en/v6/hooks/use-href
|
|
251
|
+
*/
|
|
252
|
+
|
|
253
|
+
function useHref(to) {
|
|
254
|
+
!useInRouterContext() ? process.env.NODE_ENV !== "production" ? invariant(false, // TODO: This error is probably because they somehow have 2 versions of the
|
|
255
|
+
// router loaded. We can help them understand how to avoid that.
|
|
256
|
+
"useHref() may be used only in the context of a <Router> component.") : invariant(false) : void 0;
|
|
257
|
+
let {
|
|
258
|
+
basename,
|
|
259
|
+
navigator
|
|
260
|
+
} = React.useContext(NavigationContext);
|
|
261
|
+
let {
|
|
262
|
+
hash,
|
|
263
|
+
pathname,
|
|
264
|
+
search
|
|
265
|
+
} = useResolvedPath(to);
|
|
266
|
+
let joinedPathname = pathname; // If we're operating within a basename, prepend it to the pathname prior
|
|
267
|
+
// to creating the href. If this is a root navigation, then just use the raw
|
|
268
|
+
// basename which allows the basename to have full control over the presence
|
|
269
|
+
// of a trailing slash on root links
|
|
270
|
+
|
|
271
|
+
if (basename !== "/") {
|
|
272
|
+
joinedPathname = pathname === "/" ? basename : joinPaths([basename, pathname]);
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
return navigator.createHref({
|
|
276
|
+
pathname: joinedPathname,
|
|
277
|
+
search,
|
|
278
|
+
hash
|
|
279
|
+
});
|
|
280
|
+
}
|
|
281
|
+
/**
|
|
282
|
+
* Returns true if this component is a descendant of a <Router>.
|
|
283
|
+
*
|
|
284
|
+
* @see https://reactrouter.com/docs/en/v6/hooks/use-in-router-context
|
|
285
|
+
*/
|
|
286
|
+
|
|
287
|
+
function useInRouterContext() {
|
|
288
|
+
return React.useContext(LocationContext) != null;
|
|
289
|
+
}
|
|
290
|
+
/**
|
|
291
|
+
* Returns the current location object, which represents the current URL in web
|
|
292
|
+
* browsers.
|
|
293
|
+
*
|
|
294
|
+
* Note: If you're using this it may mean you're doing some of your own
|
|
295
|
+
* "routing" in your app, and we'd like to know what your use case is. We may
|
|
296
|
+
* be able to provide something higher-level to better suit your needs.
|
|
297
|
+
*
|
|
298
|
+
* @see https://reactrouter.com/docs/en/v6/hooks/use-location
|
|
299
|
+
*/
|
|
300
|
+
|
|
301
|
+
function useLocation() {
|
|
302
|
+
!useInRouterContext() ? process.env.NODE_ENV !== "production" ? invariant(false, // TODO: This error is probably because they somehow have 2 versions of the
|
|
303
|
+
// router loaded. We can help them understand how to avoid that.
|
|
304
|
+
"useLocation() may be used only in the context of a <Router> component.") : invariant(false) : void 0;
|
|
305
|
+
return React.useContext(LocationContext).location;
|
|
306
|
+
}
|
|
307
|
+
/**
|
|
308
|
+
* Returns the current navigation action which describes how the router came to
|
|
309
|
+
* the current location, either by a pop, push, or replace on the history stack.
|
|
310
|
+
*
|
|
311
|
+
* @see https://reactrouter.com/docs/en/v6/hooks/use-navigation-type
|
|
312
|
+
*/
|
|
313
|
+
|
|
314
|
+
function useNavigationType() {
|
|
315
|
+
return React.useContext(LocationContext).navigationType;
|
|
316
|
+
}
|
|
317
|
+
/**
|
|
318
|
+
* Returns true if the URL for the given "to" value matches the current URL.
|
|
319
|
+
* This is useful for components that need to know "active" state, e.g.
|
|
320
|
+
* <NavLink>.
|
|
321
|
+
*
|
|
322
|
+
* @see https://reactrouter.com/docs/en/v6/hooks/use-match
|
|
323
|
+
*/
|
|
324
|
+
|
|
325
|
+
function useMatch(pattern) {
|
|
326
|
+
!useInRouterContext() ? process.env.NODE_ENV !== "production" ? invariant(false, // TODO: This error is probably because they somehow have 2 versions of the
|
|
327
|
+
// router loaded. We can help them understand how to avoid that.
|
|
328
|
+
"useMatch() may be used only in the context of a <Router> component.") : invariant(false) : void 0;
|
|
329
|
+
let {
|
|
330
|
+
pathname
|
|
331
|
+
} = useLocation();
|
|
332
|
+
return React.useMemo(() => matchPath(pattern, pathname), [pathname, pattern]);
|
|
333
|
+
}
|
|
334
|
+
/**
|
|
335
|
+
* The interface for the navigate() function returned from useNavigate().
|
|
336
|
+
*/
|
|
337
|
+
|
|
338
|
+
/**
|
|
339
|
+
* When processing relative navigation we want to ignore ancestor routes that
|
|
340
|
+
* do not contribute to the path, such that index/pathless layout routes don't
|
|
341
|
+
* interfere.
|
|
342
|
+
*
|
|
343
|
+
* For example, when moving a route element into an index route and/or a
|
|
344
|
+
* pathless layout route, relative link behavior contained within should stay
|
|
345
|
+
* the same. Both of the following examples should link back to the root:
|
|
346
|
+
*
|
|
347
|
+
* <Route path="/">
|
|
348
|
+
* <Route path="accounts" element={<Link to=".."}>
|
|
349
|
+
* </Route>
|
|
350
|
+
*
|
|
351
|
+
* <Route path="/">
|
|
352
|
+
* <Route path="accounts">
|
|
353
|
+
* <Route element={<AccountsLayout />}> // <-- Does not contribute
|
|
354
|
+
* <Route index element={<Link to=".."} /> // <-- Does not contribute
|
|
355
|
+
* </Route
|
|
356
|
+
* </Route>
|
|
357
|
+
* </Route>
|
|
358
|
+
*/
|
|
359
|
+
function getPathContributingMatches(matches) {
|
|
360
|
+
return matches.filter((match, index) => index === 0 || !match.route.index && match.pathnameBase !== matches[index - 1].pathnameBase);
|
|
361
|
+
}
|
|
362
|
+
/**
|
|
363
|
+
* Returns an imperative method for changing the location. Used by <Link>s, but
|
|
364
|
+
* may also be used by other elements to change the location.
|
|
365
|
+
*
|
|
366
|
+
* @see https://reactrouter.com/docs/en/v6/hooks/use-navigate
|
|
367
|
+
*/
|
|
368
|
+
|
|
369
|
+
|
|
370
|
+
function useNavigate() {
|
|
371
|
+
!useInRouterContext() ? process.env.NODE_ENV !== "production" ? invariant(false, // TODO: This error is probably because they somehow have 2 versions of the
|
|
372
|
+
// router loaded. We can help them understand how to avoid that.
|
|
373
|
+
"useNavigate() may be used only in the context of a <Router> component.") : invariant(false) : void 0;
|
|
374
|
+
let {
|
|
375
|
+
basename,
|
|
376
|
+
navigator
|
|
377
|
+
} = React.useContext(NavigationContext);
|
|
378
|
+
let {
|
|
379
|
+
matches
|
|
380
|
+
} = React.useContext(RouteContext);
|
|
381
|
+
let {
|
|
382
|
+
pathname: locationPathname
|
|
383
|
+
} = useLocation();
|
|
384
|
+
let routePathnamesJson = JSON.stringify(getPathContributingMatches(matches).map(match => match.pathnameBase));
|
|
385
|
+
let activeRef = React.useRef(false);
|
|
386
|
+
React.useEffect(() => {
|
|
387
|
+
activeRef.current = true;
|
|
388
|
+
});
|
|
389
|
+
let navigate = React.useCallback(function (to, options) {
|
|
390
|
+
if (options === void 0) {
|
|
391
|
+
options = {};
|
|
392
|
+
}
|
|
393
|
+
|
|
394
|
+
process.env.NODE_ENV !== "production" ? warning(activeRef.current, "You should call navigate() in a React.useEffect(), not when " + "your component is first rendered.") : void 0;
|
|
395
|
+
if (!activeRef.current) return;
|
|
396
|
+
|
|
397
|
+
if (typeof to === "number") {
|
|
398
|
+
navigator.go(to);
|
|
399
|
+
return;
|
|
400
|
+
}
|
|
401
|
+
|
|
402
|
+
let path = resolveTo(to, JSON.parse(routePathnamesJson), locationPathname); // If we're operating within a basename, prepend it to the pathname prior
|
|
403
|
+
// to handing off to history. If this is a root navigation, then we
|
|
404
|
+
// navigate to the raw basename which allows the basename to have full
|
|
405
|
+
// control over the presence of a trailing slash on root links
|
|
406
|
+
|
|
407
|
+
if (basename !== "/") {
|
|
408
|
+
path.pathname = path.pathname === "/" ? basename : joinPaths([basename, path.pathname]);
|
|
409
|
+
}
|
|
410
|
+
|
|
411
|
+
(!!options.replace ? navigator.replace : navigator.push)(path, options.state, options);
|
|
412
|
+
}, [basename, navigator, routePathnamesJson, locationPathname]);
|
|
413
|
+
return navigate;
|
|
414
|
+
}
|
|
415
|
+
const OutletContext = /*#__PURE__*/React.createContext(null);
|
|
416
|
+
/**
|
|
417
|
+
* Returns the context (if provided) for the child route at this level of the route
|
|
418
|
+
* hierarchy.
|
|
419
|
+
* @see https://reactrouter.com/docs/en/v6/hooks/use-outlet-context
|
|
420
|
+
*/
|
|
421
|
+
|
|
422
|
+
function useOutletContext() {
|
|
423
|
+
return React.useContext(OutletContext);
|
|
424
|
+
}
|
|
425
|
+
/**
|
|
426
|
+
* Returns the element for the child route at this level of the route
|
|
427
|
+
* hierarchy. Used internally by <Outlet> to render child routes.
|
|
428
|
+
*
|
|
429
|
+
* @see https://reactrouter.com/docs/en/v6/hooks/use-outlet
|
|
430
|
+
*/
|
|
431
|
+
|
|
432
|
+
function useOutlet(context) {
|
|
433
|
+
let outlet = React.useContext(RouteContext).outlet;
|
|
434
|
+
|
|
435
|
+
if (outlet) {
|
|
436
|
+
return /*#__PURE__*/React.createElement(OutletContext.Provider, {
|
|
437
|
+
value: context
|
|
438
|
+
}, outlet);
|
|
439
|
+
}
|
|
440
|
+
|
|
441
|
+
return outlet;
|
|
442
|
+
}
|
|
443
|
+
/**
|
|
444
|
+
* Returns an object of key/value pairs of the dynamic params from the current
|
|
445
|
+
* URL that were matched by the route path.
|
|
446
|
+
*
|
|
447
|
+
* @see https://reactrouter.com/docs/en/v6/hooks/use-params
|
|
448
|
+
*/
|
|
449
|
+
|
|
450
|
+
function useParams() {
|
|
451
|
+
let {
|
|
452
|
+
matches
|
|
453
|
+
} = React.useContext(RouteContext);
|
|
454
|
+
let routeMatch = matches[matches.length - 1];
|
|
455
|
+
return routeMatch ? routeMatch.params : {};
|
|
456
|
+
}
|
|
457
|
+
/**
|
|
458
|
+
* Resolves the pathname of the given `to` value against the current location.
|
|
459
|
+
*
|
|
460
|
+
* @see https://reactrouter.com/docs/en/v6/hooks/use-resolved-path
|
|
461
|
+
*/
|
|
462
|
+
|
|
463
|
+
function useResolvedPath(to) {
|
|
464
|
+
let {
|
|
465
|
+
matches
|
|
466
|
+
} = React.useContext(RouteContext);
|
|
467
|
+
let {
|
|
468
|
+
pathname: locationPathname
|
|
469
|
+
} = useLocation();
|
|
470
|
+
let routePathnamesJson = JSON.stringify(getPathContributingMatches(matches).map(match => match.pathnameBase));
|
|
471
|
+
return React.useMemo(() => resolveTo(to, JSON.parse(routePathnamesJson), locationPathname), [to, routePathnamesJson, locationPathname]);
|
|
472
|
+
}
|
|
473
|
+
/**
|
|
474
|
+
* Returns the element of the route that matched the current location, prepared
|
|
475
|
+
* with the correct context to render the remainder of the route tree. Route
|
|
476
|
+
* elements in the tree must render an <Outlet> to render their child route's
|
|
477
|
+
* element.
|
|
478
|
+
*
|
|
479
|
+
* @see https://reactrouter.com/docs/en/v6/hooks/use-routes
|
|
480
|
+
*/
|
|
481
|
+
|
|
482
|
+
function useRoutes(routes, locationArg) {
|
|
483
|
+
!useInRouterContext() ? process.env.NODE_ENV !== "production" ? invariant(false, // TODO: This error is probably because they somehow have 2 versions of the
|
|
484
|
+
// router loaded. We can help them understand how to avoid that.
|
|
485
|
+
"useRoutes() may be used only in the context of a <Router> component.") : invariant(false) : void 0;
|
|
486
|
+
let dataRouterStateContext = React.useContext(DataRouterStateContext);
|
|
487
|
+
let {
|
|
488
|
+
matches: parentMatches
|
|
489
|
+
} = React.useContext(RouteContext);
|
|
490
|
+
let routeMatch = parentMatches[parentMatches.length - 1];
|
|
491
|
+
let parentParams = routeMatch ? routeMatch.params : {};
|
|
492
|
+
let parentPathname = routeMatch ? routeMatch.pathname : "/";
|
|
493
|
+
let parentPathnameBase = routeMatch ? routeMatch.pathnameBase : "/";
|
|
494
|
+
let parentRoute = routeMatch && routeMatch.route;
|
|
495
|
+
|
|
496
|
+
if (process.env.NODE_ENV !== "production") {
|
|
497
|
+
// You won't get a warning about 2 different <Routes> under a <Route>
|
|
498
|
+
// without a trailing *, but this is a best-effort warning anyway since we
|
|
499
|
+
// cannot even give the warning unless they land at the parent route.
|
|
500
|
+
//
|
|
501
|
+
// Example:
|
|
502
|
+
//
|
|
503
|
+
// <Routes>
|
|
504
|
+
// {/* This route path MUST end with /* because otherwise
|
|
505
|
+
// it will never match /blog/post/123 */}
|
|
506
|
+
// <Route path="blog" element={<Blog />} />
|
|
507
|
+
// <Route path="blog/feed" element={<BlogFeed />} />
|
|
508
|
+
// </Routes>
|
|
509
|
+
//
|
|
510
|
+
// function Blog() {
|
|
511
|
+
// return (
|
|
512
|
+
// <Routes>
|
|
513
|
+
// <Route path="post/:id" element={<Post />} />
|
|
514
|
+
// </Routes>
|
|
515
|
+
// );
|
|
516
|
+
// }
|
|
517
|
+
let parentPath = parentRoute && parentRoute.path || "";
|
|
518
|
+
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 + "/*") + "\">."));
|
|
519
|
+
}
|
|
520
|
+
|
|
521
|
+
let locationFromContext = useLocation();
|
|
522
|
+
let location;
|
|
523
|
+
|
|
524
|
+
if (locationArg) {
|
|
525
|
+
var _parsedLocationArg$pa;
|
|
526
|
+
|
|
527
|
+
let parsedLocationArg = typeof locationArg === "string" ? parsePath(locationArg) : locationArg;
|
|
528
|
+
!(parentPathnameBase === "/" || ((_parsedLocationArg$pa = parsedLocationArg.pathname) == null ? void 0 : _parsedLocationArg$pa.startsWith(parentPathnameBase))) ? process.env.NODE_ENV !== "production" ? invariant(false, "When overriding the location using `<Routes location>` or `useRoutes(routes, location)`, " + "the location pathname must begin with the portion of the URL pathname that was " + ("matched by all parent routes. The current pathname base is \"" + parentPathnameBase + "\" ") + ("but pathname \"" + parsedLocationArg.pathname + "\" was given in the `location` prop.")) : invariant(false) : void 0;
|
|
529
|
+
location = parsedLocationArg;
|
|
530
|
+
} else {
|
|
531
|
+
location = locationFromContext;
|
|
532
|
+
}
|
|
533
|
+
|
|
534
|
+
let pathname = location.pathname || "/";
|
|
535
|
+
let remainingPathname = parentPathnameBase === "/" ? pathname : pathname.slice(parentPathnameBase.length) || "/";
|
|
536
|
+
let matches = matchRoutes(routes, {
|
|
537
|
+
pathname: remainingPathname
|
|
538
|
+
});
|
|
539
|
+
|
|
540
|
+
if (process.env.NODE_ENV !== "production") {
|
|
541
|
+
process.env.NODE_ENV !== "production" ? warning(parentRoute || matches != null, "No routes matched location \"" + location.pathname + location.search + location.hash + "\" ") : void 0;
|
|
542
|
+
process.env.NODE_ENV !== "production" ? warning(matches == null || matches[matches.length - 1].route.element !== undefined, "Matched leaf route at location \"" + location.pathname + location.search + location.hash + "\" does not have an element. " + "This means it will render an <Outlet /> with a null value by default resulting in an \"empty\" page.") : void 0;
|
|
543
|
+
}
|
|
544
|
+
|
|
545
|
+
return _renderMatches(matches && matches.map(match => Object.assign({}, match, {
|
|
546
|
+
params: Object.assign({}, parentParams, match.params),
|
|
547
|
+
pathname: joinPaths([parentPathnameBase, match.pathname]),
|
|
548
|
+
pathnameBase: match.pathnameBase === "/" ? parentPathnameBase : joinPaths([parentPathnameBase, match.pathnameBase])
|
|
549
|
+
})), parentMatches, dataRouterStateContext || undefined);
|
|
550
|
+
}
|
|
551
|
+
|
|
552
|
+
function DefaultErrorElement() {
|
|
553
|
+
let error = useRouteError();
|
|
554
|
+
let message = isRouteErrorResponse(error) ? error.status + " " + error.statusText : error instanceof Error ? error.message : JSON.stringify(error);
|
|
555
|
+
let stack = error instanceof Error ? error.stack : null;
|
|
556
|
+
let lightgrey = "rgba(200,200,200, 0.5)";
|
|
557
|
+
let preStyles = {
|
|
558
|
+
padding: "0.5rem",
|
|
559
|
+
backgroundColor: lightgrey
|
|
560
|
+
};
|
|
561
|
+
let codeStyles = {
|
|
562
|
+
padding: "2px 4px",
|
|
563
|
+
backgroundColor: lightgrey
|
|
564
|
+
};
|
|
565
|
+
return /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement("h2", null, "Unhandled Thrown Error!"), /*#__PURE__*/React.createElement("h3", {
|
|
566
|
+
style: {
|
|
567
|
+
fontStyle: "italic"
|
|
568
|
+
}
|
|
569
|
+
}, message), stack ? /*#__PURE__*/React.createElement("pre", {
|
|
570
|
+
style: preStyles
|
|
571
|
+
}, stack) : null, /*#__PURE__*/React.createElement("p", null, "\uD83D\uDCBF Hey developer \uD83D\uDC4B"), /*#__PURE__*/React.createElement("p", null, "You can provide a way better UX than this when your app throws errors by providing your own\xA0", /*#__PURE__*/React.createElement("code", {
|
|
572
|
+
style: codeStyles
|
|
573
|
+
}, "errorElement"), " props on\xA0", /*#__PURE__*/React.createElement("code", {
|
|
574
|
+
style: codeStyles
|
|
575
|
+
}, "<Route>")));
|
|
576
|
+
}
|
|
577
|
+
|
|
578
|
+
class RenderErrorBoundary extends React.Component {
|
|
579
|
+
constructor(props) {
|
|
580
|
+
super(props);
|
|
581
|
+
this.state = {
|
|
582
|
+
location: props.location,
|
|
583
|
+
error: props.error
|
|
584
|
+
};
|
|
585
|
+
}
|
|
586
|
+
|
|
587
|
+
static getDerivedStateFromError(error) {
|
|
588
|
+
return {
|
|
589
|
+
error: error
|
|
590
|
+
};
|
|
591
|
+
}
|
|
592
|
+
|
|
593
|
+
static getDerivedStateFromProps(props, state) {
|
|
594
|
+
// When we get into an error state, the user will likely click "back" to the
|
|
595
|
+
// previous page that didn't have an error. Because this wraps the entire
|
|
596
|
+
// application, that will have no effect--the error page continues to display.
|
|
597
|
+
// This gives us a mechanism to recover from the error when the location changes.
|
|
598
|
+
//
|
|
599
|
+
// Whether we're in an error state or not, we update the location in state
|
|
600
|
+
// so that when we are in an error state, it gets reset when a new location
|
|
601
|
+
// comes in and the user recovers from the error.
|
|
602
|
+
if (state.location !== props.location) {
|
|
603
|
+
return {
|
|
604
|
+
error: props.error,
|
|
605
|
+
location: props.location
|
|
606
|
+
};
|
|
607
|
+
} // If we're not changing locations, preserve the location but still surface
|
|
608
|
+
// any new errors that may come through. We retain the existing error, we do
|
|
609
|
+
// this because the error provided from the app state may be cleared without
|
|
610
|
+
// the location changing.
|
|
611
|
+
|
|
612
|
+
|
|
613
|
+
return {
|
|
614
|
+
error: props.error || state.error,
|
|
615
|
+
location: state.location
|
|
616
|
+
};
|
|
617
|
+
}
|
|
618
|
+
|
|
619
|
+
componentDidCatch(error, errorInfo) {
|
|
620
|
+
console.error("React Router caught the following error during render", error, errorInfo);
|
|
621
|
+
}
|
|
622
|
+
|
|
623
|
+
render() {
|
|
624
|
+
return this.state.error ? /*#__PURE__*/React.createElement(RouteErrorContext.Provider, {
|
|
625
|
+
value: this.state.error,
|
|
626
|
+
children: this.props.component
|
|
627
|
+
}) : this.props.children;
|
|
628
|
+
}
|
|
629
|
+
|
|
630
|
+
}
|
|
631
|
+
|
|
632
|
+
function RenderedRoute(_ref) {
|
|
633
|
+
let {
|
|
634
|
+
routeContext,
|
|
635
|
+
match,
|
|
636
|
+
children
|
|
637
|
+
} = _ref;
|
|
638
|
+
let dataStaticRouterContext = React.useContext(DataStaticRouterContext); // Track how deep we got in our render pass to emulate SSR componentDidCatch
|
|
639
|
+
// in a DataStaticRouter
|
|
640
|
+
|
|
641
|
+
if (dataStaticRouterContext && match.route.errorElement) {
|
|
642
|
+
dataStaticRouterContext._deepestRenderedBoundaryId = match.route.id;
|
|
643
|
+
}
|
|
644
|
+
|
|
645
|
+
return /*#__PURE__*/React.createElement(RouteContext.Provider, {
|
|
646
|
+
value: routeContext
|
|
647
|
+
}, children);
|
|
648
|
+
}
|
|
649
|
+
|
|
650
|
+
function _renderMatches(matches, parentMatches, dataRouterState) {
|
|
651
|
+
if (parentMatches === void 0) {
|
|
652
|
+
parentMatches = [];
|
|
653
|
+
}
|
|
654
|
+
|
|
655
|
+
if (matches == null) {
|
|
656
|
+
if (dataRouterState != null && dataRouterState.errors) {
|
|
657
|
+
// Don't bail if we have data router errors so we can render them in the
|
|
658
|
+
// boundary. Use the pre-matched (or shimmed) matches
|
|
659
|
+
matches = dataRouterState.matches;
|
|
660
|
+
} else {
|
|
661
|
+
return null;
|
|
662
|
+
}
|
|
663
|
+
}
|
|
664
|
+
|
|
665
|
+
let renderedMatches = matches; // If we have data errors, trim matches to the highest error boundary
|
|
666
|
+
|
|
667
|
+
let errors = dataRouterState == null ? void 0 : dataRouterState.errors;
|
|
668
|
+
|
|
669
|
+
if (errors != null) {
|
|
670
|
+
let errorIndex = renderedMatches.findIndex(m => m.route.id && (errors == null ? void 0 : errors[m.route.id]));
|
|
671
|
+
!(errorIndex >= 0) ? process.env.NODE_ENV !== "production" ? invariant(false, "Could not find a matching route for the current errors: " + errors) : invariant(false) : void 0;
|
|
672
|
+
renderedMatches = renderedMatches.slice(0, Math.min(renderedMatches.length, errorIndex + 1));
|
|
673
|
+
}
|
|
674
|
+
|
|
675
|
+
return renderedMatches.reduceRight((outlet, match, index) => {
|
|
676
|
+
let error = match.route.id ? errors == null ? void 0 : errors[match.route.id] : null; // Only data routers handle errors
|
|
677
|
+
|
|
678
|
+
let errorElement = dataRouterState ? match.route.errorElement || /*#__PURE__*/React.createElement(DefaultErrorElement, null) : null;
|
|
679
|
+
|
|
680
|
+
let getChildren = () => /*#__PURE__*/React.createElement(RenderedRoute, {
|
|
681
|
+
match: match,
|
|
682
|
+
routeContext: {
|
|
683
|
+
outlet,
|
|
684
|
+
matches: parentMatches.concat(renderedMatches.slice(0, index + 1))
|
|
685
|
+
}
|
|
686
|
+
}, error ? errorElement : match.route.element !== undefined ? match.route.element : outlet); // Only wrap in an error boundary within data router usages when we have an
|
|
687
|
+
// errorElement on this route. Otherwise let it bubble up to an ancestor
|
|
688
|
+
// errorElement
|
|
689
|
+
|
|
690
|
+
|
|
691
|
+
return dataRouterState && (match.route.errorElement || index === 0) ? /*#__PURE__*/React.createElement(RenderErrorBoundary, {
|
|
692
|
+
location: dataRouterState.location,
|
|
693
|
+
component: errorElement,
|
|
694
|
+
error: error,
|
|
695
|
+
children: getChildren()
|
|
696
|
+
}) : getChildren();
|
|
697
|
+
}, null);
|
|
698
|
+
}
|
|
699
|
+
var DataRouterHook;
|
|
700
|
+
|
|
701
|
+
(function (DataRouterHook) {
|
|
702
|
+
DataRouterHook["UseLoaderData"] = "useLoaderData";
|
|
703
|
+
DataRouterHook["UseActionData"] = "useActionData";
|
|
704
|
+
DataRouterHook["UseRouteError"] = "useRouteError";
|
|
705
|
+
DataRouterHook["UseNavigation"] = "useNavigation";
|
|
706
|
+
DataRouterHook["UseRouteLoaderData"] = "useRouteLoaderData";
|
|
707
|
+
DataRouterHook["UseMatches"] = "useMatches";
|
|
708
|
+
DataRouterHook["UseRevalidator"] = "useRevalidator";
|
|
709
|
+
})(DataRouterHook || (DataRouterHook = {}));
|
|
710
|
+
|
|
711
|
+
function useDataRouterState(hookName) {
|
|
712
|
+
let state = React.useContext(DataRouterStateContext);
|
|
713
|
+
!state ? process.env.NODE_ENV !== "production" ? invariant(false, hookName + " must be used within a DataRouterStateContext") : invariant(false) : void 0;
|
|
714
|
+
return state;
|
|
715
|
+
}
|
|
716
|
+
/**
|
|
717
|
+
* Returns the current navigation, defaulting to an "idle" navigation when
|
|
718
|
+
* no navigation is in progress
|
|
719
|
+
*/
|
|
720
|
+
|
|
721
|
+
|
|
722
|
+
function useNavigation() {
|
|
723
|
+
let state = useDataRouterState(DataRouterHook.UseNavigation);
|
|
724
|
+
return state.navigation;
|
|
725
|
+
}
|
|
726
|
+
/**
|
|
727
|
+
* Returns a revalidate function for manually triggering revalidation, as well
|
|
728
|
+
* as the current state of any manual revalidations
|
|
729
|
+
*/
|
|
730
|
+
|
|
731
|
+
function useRevalidator() {
|
|
732
|
+
let dataRouterContext = React.useContext(DataRouterContext);
|
|
733
|
+
!dataRouterContext ? process.env.NODE_ENV !== "production" ? invariant(false, "useRevalidator must be used within a DataRouterContext") : invariant(false) : void 0;
|
|
734
|
+
let state = useDataRouterState(DataRouterHook.UseRevalidator);
|
|
735
|
+
return {
|
|
736
|
+
revalidate: dataRouterContext.router.revalidate,
|
|
737
|
+
state: state.revalidation
|
|
738
|
+
};
|
|
739
|
+
}
|
|
740
|
+
/**
|
|
741
|
+
* Returns the active route matches, useful for accessing loaderData for
|
|
742
|
+
* parent/child routes or the route "handle" property
|
|
743
|
+
*/
|
|
744
|
+
|
|
745
|
+
function useMatches() {
|
|
746
|
+
let {
|
|
747
|
+
matches,
|
|
748
|
+
loaderData
|
|
749
|
+
} = useDataRouterState(DataRouterHook.UseMatches);
|
|
750
|
+
return React.useMemo(() => matches.map(match => {
|
|
751
|
+
let {
|
|
752
|
+
pathname,
|
|
753
|
+
params
|
|
754
|
+
} = match;
|
|
755
|
+
return {
|
|
756
|
+
id: match.route.id,
|
|
757
|
+
pathname,
|
|
758
|
+
params,
|
|
759
|
+
data: loaderData[match.route.id],
|
|
760
|
+
handle: match.route.handle
|
|
761
|
+
};
|
|
762
|
+
}), [matches, loaderData]);
|
|
763
|
+
}
|
|
764
|
+
/**
|
|
765
|
+
* Returns the loader data for the nearest ancestor Route loader
|
|
766
|
+
*/
|
|
767
|
+
|
|
768
|
+
function useLoaderData() {
|
|
769
|
+
let state = useDataRouterState(DataRouterHook.UseLoaderData);
|
|
770
|
+
let route = React.useContext(RouteContext);
|
|
771
|
+
!route ? process.env.NODE_ENV !== "production" ? invariant(false, "useLoaderData must be used inside a RouteContext") : invariant(false) : void 0;
|
|
772
|
+
let thisRoute = route.matches[route.matches.length - 1];
|
|
773
|
+
!thisRoute.route.id ? process.env.NODE_ENV !== "production" ? invariant(false, "useLoaderData can only be used on routes that contain a unique \"id\"") : invariant(false) : void 0;
|
|
774
|
+
return state.loaderData[thisRoute.route.id];
|
|
775
|
+
}
|
|
776
|
+
/**
|
|
777
|
+
* Returns the loaderData for the given routeId
|
|
778
|
+
*/
|
|
779
|
+
|
|
780
|
+
function useRouteLoaderData(routeId) {
|
|
781
|
+
let state = useDataRouterState(DataRouterHook.UseRouteLoaderData);
|
|
782
|
+
return state.loaderData[routeId];
|
|
783
|
+
}
|
|
784
|
+
/**
|
|
785
|
+
* Returns the action data for the nearest ancestor Route action
|
|
786
|
+
*/
|
|
787
|
+
|
|
788
|
+
function useActionData() {
|
|
789
|
+
let state = useDataRouterState(DataRouterHook.UseActionData);
|
|
790
|
+
let route = React.useContext(RouteContext);
|
|
791
|
+
!route ? process.env.NODE_ENV !== "production" ? invariant(false, "useActionData must be used inside a RouteContext") : invariant(false) : void 0;
|
|
792
|
+
return Object.values((state == null ? void 0 : state.actionData) || {})[0];
|
|
793
|
+
}
|
|
794
|
+
/**
|
|
795
|
+
* Returns the nearest ancestor Route error, which could be a loader/action
|
|
796
|
+
* error or a render error. This is intended to be called from your
|
|
797
|
+
* errorElement to display a proper error message.
|
|
798
|
+
*/
|
|
799
|
+
|
|
800
|
+
function useRouteError() {
|
|
801
|
+
var _state$errors;
|
|
802
|
+
|
|
803
|
+
let error = React.useContext(RouteErrorContext);
|
|
804
|
+
let state = useDataRouterState(DataRouterHook.UseRouteError);
|
|
805
|
+
let route = React.useContext(RouteContext);
|
|
806
|
+
let thisRoute = route.matches[route.matches.length - 1];
|
|
807
|
+
let deferredValue = React.useContext(DeferredContext); // Return deferred errors if we're inside a Deferred errorElement
|
|
808
|
+
|
|
809
|
+
if (deferredValue && deferredValue instanceof Error) {
|
|
810
|
+
return deferredValue;
|
|
811
|
+
} // If this was a render error, we put it in a RouteError context inside
|
|
812
|
+
// of RenderErrorBoundary
|
|
813
|
+
|
|
814
|
+
|
|
815
|
+
if (error) {
|
|
816
|
+
return error;
|
|
817
|
+
}
|
|
818
|
+
|
|
819
|
+
!route ? process.env.NODE_ENV !== "production" ? invariant(false, "useRouteError must be used inside a RouteContext") : invariant(false) : void 0;
|
|
820
|
+
!thisRoute.route.id ? process.env.NODE_ENV !== "production" ? invariant(false, "useRouteError can only be used on routes that contain a unique \"id\"") : invariant(false) : void 0; // Otherwise look for errors from our data router state
|
|
821
|
+
|
|
822
|
+
return (_state$errors = state.errors) == null ? void 0 : _state$errors[thisRoute.route.id];
|
|
823
|
+
}
|
|
824
|
+
/**
|
|
825
|
+
* Returns the happy-path data from the nearest ancestor <Deferred /> value
|
|
826
|
+
*/
|
|
827
|
+
|
|
828
|
+
function useDeferredData() {
|
|
829
|
+
let value = React.useContext(DeferredContext);
|
|
830
|
+
return value;
|
|
831
|
+
}
|
|
832
|
+
const alreadyWarned = {};
|
|
833
|
+
|
|
834
|
+
function warningOnce(key, cond, message) {
|
|
835
|
+
if (!cond && !alreadyWarned[key]) {
|
|
836
|
+
alreadyWarned[key] = true;
|
|
837
|
+
process.env.NODE_ENV !== "production" ? warning(false, message) : void 0;
|
|
838
|
+
}
|
|
839
|
+
}
|
|
840
|
+
|
|
841
|
+
// to avoid issues w.r.t. dual initialization fetches in concurrent rendering.
|
|
842
|
+
// Data router apps are expected to have a static route tree and are not intended
|
|
843
|
+
// to be unmounted/remounted at runtime.
|
|
844
|
+
|
|
845
|
+
let routerSingleton;
|
|
846
|
+
/**
|
|
847
|
+
* A higher-order component that, given a Remix Router instance. setups the
|
|
848
|
+
* Context's required for data routing
|
|
849
|
+
*/
|
|
850
|
+
|
|
851
|
+
function DataRouterProvider(_ref) {
|
|
852
|
+
let {
|
|
853
|
+
basename,
|
|
854
|
+
children,
|
|
855
|
+
fallbackElement,
|
|
856
|
+
router
|
|
857
|
+
} = _ref;
|
|
858
|
+
// Sync router state to our component state to force re-renders
|
|
859
|
+
let state = useSyncExternalStore(router.subscribe, () => router.state, // We have to provide this so React@18 doesn't complain during hydration,
|
|
860
|
+
// but we pass our serialized hydration data into the router so state here
|
|
861
|
+
// is already synced with what the server saw
|
|
862
|
+
() => router.state);
|
|
863
|
+
let navigator = React.useMemo(() => {
|
|
864
|
+
return {
|
|
865
|
+
createHref: router.createHref,
|
|
866
|
+
go: n => router.navigate(n),
|
|
867
|
+
push: (to, state, opts) => router.navigate(to, {
|
|
868
|
+
state,
|
|
869
|
+
resetScroll: opts == null ? void 0 : opts.resetScroll
|
|
870
|
+
}),
|
|
871
|
+
replace: (to, state, opts) => router.navigate(to, {
|
|
872
|
+
replace: true,
|
|
873
|
+
state,
|
|
874
|
+
resetScroll: opts == null ? void 0 : opts.resetScroll
|
|
875
|
+
})
|
|
876
|
+
};
|
|
877
|
+
}, [router]);
|
|
878
|
+
let dataRouterContext = {
|
|
879
|
+
router,
|
|
880
|
+
navigator,
|
|
881
|
+
static: false,
|
|
882
|
+
basename: basename || "/"
|
|
883
|
+
};
|
|
884
|
+
|
|
885
|
+
if (!state.initialized) {
|
|
886
|
+
return /*#__PURE__*/React.createElement(React.Fragment, null, fallbackElement);
|
|
887
|
+
}
|
|
888
|
+
|
|
889
|
+
return /*#__PURE__*/React.createElement(DataRouterContext.Provider, {
|
|
890
|
+
value: dataRouterContext
|
|
891
|
+
}, /*#__PURE__*/React.createElement(DataRouterStateContext.Provider, {
|
|
892
|
+
value: state,
|
|
893
|
+
children: children
|
|
894
|
+
}));
|
|
895
|
+
}
|
|
896
|
+
/**
|
|
897
|
+
* A data-aware wrapper for `<Router>` that leverages the Context's provided by
|
|
898
|
+
* `<DataRouterProvider>`
|
|
899
|
+
*/
|
|
900
|
+
|
|
901
|
+
function DataRouter() {
|
|
902
|
+
let dataRouterContext = React.useContext(DataRouterContext);
|
|
903
|
+
!dataRouterContext ? process.env.NODE_ENV !== "production" ? invariant(false, "<DataRouter> may only be rendered within a DataRouterContext") : invariant(false) : void 0;
|
|
904
|
+
let {
|
|
905
|
+
router,
|
|
906
|
+
navigator,
|
|
907
|
+
basename
|
|
908
|
+
} = dataRouterContext;
|
|
909
|
+
return /*#__PURE__*/React.createElement(Router, {
|
|
910
|
+
basename: basename,
|
|
911
|
+
location: router.state.location,
|
|
912
|
+
navigationType: router.state.historyAction,
|
|
913
|
+
navigator: navigator
|
|
914
|
+
}, /*#__PURE__*/React.createElement(Routes, null));
|
|
915
|
+
}
|
|
916
|
+
function DataMemoryRouter(_ref2) {
|
|
917
|
+
let {
|
|
918
|
+
basename,
|
|
919
|
+
children,
|
|
920
|
+
initialEntries,
|
|
921
|
+
initialIndex,
|
|
922
|
+
hydrationData,
|
|
923
|
+
fallbackElement,
|
|
924
|
+
routes
|
|
925
|
+
} = _ref2;
|
|
926
|
+
|
|
927
|
+
if (!routerSingleton) {
|
|
928
|
+
routerSingleton = createMemoryRouter({
|
|
929
|
+
basename,
|
|
930
|
+
hydrationData,
|
|
931
|
+
initialEntries,
|
|
932
|
+
initialIndex,
|
|
933
|
+
routes: routes || createRoutesFromChildren(children)
|
|
934
|
+
}).initialize();
|
|
935
|
+
}
|
|
936
|
+
|
|
937
|
+
let router = routerSingleton;
|
|
938
|
+
return /*#__PURE__*/React.createElement(DataRouterProvider, {
|
|
939
|
+
router: router,
|
|
940
|
+
basename: basename,
|
|
941
|
+
fallbackElement: fallbackElement
|
|
942
|
+
}, /*#__PURE__*/React.createElement(DataRouter, null));
|
|
943
|
+
}
|
|
944
|
+
|
|
945
|
+
/**
|
|
946
|
+
* A <Router> that stores all entries in memory.
|
|
947
|
+
*
|
|
948
|
+
* @see https://reactrouter.com/docs/en/v6/routers/memory-router
|
|
949
|
+
*/
|
|
950
|
+
function MemoryRouter(_ref3) {
|
|
951
|
+
let {
|
|
952
|
+
basename,
|
|
953
|
+
children,
|
|
954
|
+
initialEntries,
|
|
955
|
+
initialIndex
|
|
956
|
+
} = _ref3;
|
|
957
|
+
let historyRef = React.useRef();
|
|
958
|
+
|
|
959
|
+
if (historyRef.current == null) {
|
|
960
|
+
historyRef.current = createMemoryHistory({
|
|
961
|
+
initialEntries,
|
|
962
|
+
initialIndex,
|
|
963
|
+
v5Compat: true
|
|
964
|
+
});
|
|
965
|
+
}
|
|
966
|
+
|
|
967
|
+
let history = historyRef.current;
|
|
968
|
+
let [state, setState] = React.useState({
|
|
969
|
+
action: history.action,
|
|
970
|
+
location: history.location
|
|
971
|
+
});
|
|
972
|
+
React.useLayoutEffect(() => history.listen(setState), [history]);
|
|
973
|
+
return /*#__PURE__*/React.createElement(Router, {
|
|
974
|
+
basename: basename,
|
|
975
|
+
children: children,
|
|
976
|
+
location: state.location,
|
|
977
|
+
navigationType: state.action,
|
|
978
|
+
navigator: history
|
|
979
|
+
});
|
|
980
|
+
}
|
|
981
|
+
|
|
982
|
+
/**
|
|
983
|
+
* Changes the current location.
|
|
984
|
+
*
|
|
985
|
+
* Note: This API is mostly useful in React.Component subclasses that are not
|
|
986
|
+
* able to use hooks. In functional components, we recommend you use the
|
|
987
|
+
* `useNavigate` hook instead.
|
|
988
|
+
*
|
|
989
|
+
* @see https://reactrouter.com/docs/en/v6/components/navigate
|
|
990
|
+
*/
|
|
991
|
+
function Navigate(_ref4) {
|
|
992
|
+
let {
|
|
993
|
+
to,
|
|
994
|
+
replace,
|
|
995
|
+
state
|
|
996
|
+
} = _ref4;
|
|
997
|
+
!useInRouterContext() ? process.env.NODE_ENV !== "production" ? invariant(false, // TODO: This error is probably because they somehow have 2 versions of
|
|
998
|
+
// the router loaded. We can help them understand how to avoid that.
|
|
999
|
+
"<Navigate> may be used only in the context of a <Router> component.") : invariant(false) : void 0;
|
|
1000
|
+
process.env.NODE_ENV !== "production" ? warning(!React.useContext(NavigationContext).static, "<Navigate> must not be used on the initial render in a <StaticRouter>. " + "This is a no-op, but you should modify your code so the <Navigate> is " + "only ever rendered in response to some user interaction or state change.") : void 0;
|
|
1001
|
+
let navigate = useNavigate();
|
|
1002
|
+
React.useEffect(() => {
|
|
1003
|
+
navigate(to, {
|
|
1004
|
+
replace,
|
|
1005
|
+
state
|
|
1006
|
+
});
|
|
1007
|
+
});
|
|
1008
|
+
return null;
|
|
1009
|
+
}
|
|
1010
|
+
|
|
1011
|
+
/**
|
|
1012
|
+
* Renders the child route's element, if there is one.
|
|
1013
|
+
*
|
|
1014
|
+
* @see https://reactrouter.com/docs/en/v6/components/outlet
|
|
1015
|
+
*/
|
|
1016
|
+
function Outlet(props) {
|
|
1017
|
+
return useOutlet(props.context);
|
|
1018
|
+
}
|
|
1019
|
+
|
|
1020
|
+
/**
|
|
1021
|
+
* Declares an element that should be rendered at a certain URL path.
|
|
1022
|
+
*
|
|
1023
|
+
* @see https://reactrouter.com/docs/en/v6/components/route
|
|
1024
|
+
*/
|
|
1025
|
+
function Route(_props) {
|
|
1026
|
+
process.env.NODE_ENV !== "production" ? invariant(false, "A <Route> is only ever to be used as the child of <Routes> element, " + "never rendered directly. Please wrap your <Route> in a <Routes>.") : invariant(false) ;
|
|
1027
|
+
}
|
|
1028
|
+
|
|
1029
|
+
/**
|
|
1030
|
+
* Provides location context for the rest of the app.
|
|
1031
|
+
*
|
|
1032
|
+
* Note: You usually won't render a <Router> directly. Instead, you'll render a
|
|
1033
|
+
* router that is more specific to your environment such as a <BrowserRouter>
|
|
1034
|
+
* in web browsers or a <StaticRouter> for server rendering.
|
|
1035
|
+
*
|
|
1036
|
+
* @see https://reactrouter.com/docs/en/v6/routers/router
|
|
1037
|
+
*/
|
|
1038
|
+
function Router(_ref5) {
|
|
1039
|
+
let {
|
|
1040
|
+
basename: basenameProp = "/",
|
|
1041
|
+
children = null,
|
|
1042
|
+
location: locationProp,
|
|
1043
|
+
navigationType = Action.Pop,
|
|
1044
|
+
navigator,
|
|
1045
|
+
static: staticProp = false
|
|
1046
|
+
} = _ref5;
|
|
1047
|
+
!!useInRouterContext() ? process.env.NODE_ENV !== "production" ? invariant(false, "You cannot render a <Router> inside another <Router>." + " You should never have more than one in your app.") : invariant(false) : void 0; // Preserve trailing slashes on basename, so we can let the user control
|
|
1048
|
+
// the enforcement of trailing slashes throughout the app
|
|
1049
|
+
|
|
1050
|
+
let basename = basenameProp.replace(/^\/*/, "/");
|
|
1051
|
+
let navigationContext = React.useMemo(() => ({
|
|
1052
|
+
basename,
|
|
1053
|
+
navigator,
|
|
1054
|
+
static: staticProp
|
|
1055
|
+
}), [basename, navigator, staticProp]);
|
|
1056
|
+
|
|
1057
|
+
if (typeof locationProp === "string") {
|
|
1058
|
+
locationProp = parsePath(locationProp);
|
|
1059
|
+
}
|
|
1060
|
+
|
|
1061
|
+
let {
|
|
1062
|
+
pathname = "/",
|
|
1063
|
+
search = "",
|
|
1064
|
+
hash = "",
|
|
1065
|
+
state = null,
|
|
1066
|
+
key = "default"
|
|
1067
|
+
} = locationProp;
|
|
1068
|
+
let location = React.useMemo(() => {
|
|
1069
|
+
let trailingPathname = stripBasename(pathname, basename);
|
|
1070
|
+
|
|
1071
|
+
if (trailingPathname == null) {
|
|
1072
|
+
return null;
|
|
1073
|
+
}
|
|
1074
|
+
|
|
1075
|
+
return {
|
|
1076
|
+
pathname: trailingPathname,
|
|
1077
|
+
search,
|
|
1078
|
+
hash,
|
|
1079
|
+
state,
|
|
1080
|
+
key
|
|
1081
|
+
};
|
|
1082
|
+
}, [basename, pathname, search, hash, state, key]);
|
|
1083
|
+
process.env.NODE_ENV !== "production" ? warning(location != null, "<Router basename=\"" + basename + "\"> is not able to match the URL " + ("\"" + pathname + search + hash + "\" because it does not start with the ") + "basename, so the <Router> won't render anything.") : void 0;
|
|
1084
|
+
|
|
1085
|
+
if (location == null) {
|
|
1086
|
+
return null;
|
|
1087
|
+
}
|
|
1088
|
+
|
|
1089
|
+
return /*#__PURE__*/React.createElement(NavigationContext.Provider, {
|
|
1090
|
+
value: navigationContext
|
|
1091
|
+
}, /*#__PURE__*/React.createElement(LocationContext.Provider, {
|
|
1092
|
+
children: children,
|
|
1093
|
+
value: {
|
|
1094
|
+
location,
|
|
1095
|
+
navigationType
|
|
1096
|
+
}
|
|
1097
|
+
}));
|
|
1098
|
+
}
|
|
1099
|
+
|
|
1100
|
+
/**
|
|
1101
|
+
* A container for a nested tree of <Route> elements that renders the branch
|
|
1102
|
+
* that best matches the current location.
|
|
1103
|
+
*
|
|
1104
|
+
* @see https://reactrouter.com/docs/en/v6/components/routes
|
|
1105
|
+
*/
|
|
1106
|
+
function Routes(_ref6) {
|
|
1107
|
+
let {
|
|
1108
|
+
children,
|
|
1109
|
+
location
|
|
1110
|
+
} = _ref6;
|
|
1111
|
+
let dataRouterContext = React.useContext(DataRouterContext); // When in a DataRouterContext _without_ children, we use the router routes
|
|
1112
|
+
// directly. If we have children, then we're in a descendant tree and we
|
|
1113
|
+
// need to use child routes.
|
|
1114
|
+
|
|
1115
|
+
let routes = dataRouterContext && !children ? dataRouterContext.router.routes : createRoutesFromChildren(children);
|
|
1116
|
+
return useRoutes(routes, location);
|
|
1117
|
+
}
|
|
1118
|
+
|
|
1119
|
+
/**
|
|
1120
|
+
* Component to use for rendering lazily loaded data from returning deferred()
|
|
1121
|
+
* in a loader function
|
|
1122
|
+
*/
|
|
1123
|
+
function Deferred(_ref7) {
|
|
1124
|
+
let {
|
|
1125
|
+
children,
|
|
1126
|
+
value,
|
|
1127
|
+
errorElement
|
|
1128
|
+
} = _ref7;
|
|
1129
|
+
return /*#__PURE__*/React.createElement(DeferredErrorBoundary, {
|
|
1130
|
+
value: value,
|
|
1131
|
+
errorElement: errorElement
|
|
1132
|
+
}, /*#__PURE__*/React.createElement(ResolveDeferred, null, children));
|
|
1133
|
+
}
|
|
1134
|
+
|
|
1135
|
+
class DeferredErrorBoundary extends React.Component {
|
|
1136
|
+
constructor(props) {
|
|
1137
|
+
super(props);
|
|
1138
|
+
this.state = {
|
|
1139
|
+
error: null
|
|
1140
|
+
};
|
|
1141
|
+
}
|
|
1142
|
+
|
|
1143
|
+
static getDerivedStateFromError(error) {
|
|
1144
|
+
return {
|
|
1145
|
+
error
|
|
1146
|
+
};
|
|
1147
|
+
}
|
|
1148
|
+
|
|
1149
|
+
componentDidCatch(error, errorInfo) {
|
|
1150
|
+
console.error("<Deferred> caught the following error during render", error, errorInfo);
|
|
1151
|
+
}
|
|
1152
|
+
|
|
1153
|
+
render() {
|
|
1154
|
+
let {
|
|
1155
|
+
children,
|
|
1156
|
+
errorElement,
|
|
1157
|
+
value
|
|
1158
|
+
} = this.props; // Handle render errors from this.state, or data errors from context
|
|
1159
|
+
|
|
1160
|
+
let error = this.state.error || (isDeferredError(value) ? value : null);
|
|
1161
|
+
|
|
1162
|
+
if (error) {
|
|
1163
|
+
if (errorElement) {
|
|
1164
|
+
// We have our own errorElement, provide our error and render it
|
|
1165
|
+
return /*#__PURE__*/React.createElement(DeferredContext.Provider, {
|
|
1166
|
+
value: error,
|
|
1167
|
+
children: errorElement
|
|
1168
|
+
});
|
|
1169
|
+
} // Throw to the nearest ancestor route-level error boundary
|
|
1170
|
+
|
|
1171
|
+
|
|
1172
|
+
throw error;
|
|
1173
|
+
}
|
|
1174
|
+
|
|
1175
|
+
if (value instanceof Promise) {
|
|
1176
|
+
// Throw to the suspense boundary
|
|
1177
|
+
throw value;
|
|
1178
|
+
} // We've resolved successfully, provide the value and render the children
|
|
1179
|
+
|
|
1180
|
+
|
|
1181
|
+
return /*#__PURE__*/React.createElement(DeferredContext.Provider, {
|
|
1182
|
+
value: value,
|
|
1183
|
+
children: children
|
|
1184
|
+
});
|
|
1185
|
+
}
|
|
1186
|
+
|
|
1187
|
+
}
|
|
1188
|
+
/**
|
|
1189
|
+
* @private
|
|
1190
|
+
* Indirection to leverage useDeferredData for a render-prop API on <Deferred>
|
|
1191
|
+
*/
|
|
1192
|
+
|
|
1193
|
+
|
|
1194
|
+
function ResolveDeferred(_ref8) {
|
|
1195
|
+
let {
|
|
1196
|
+
children
|
|
1197
|
+
} = _ref8;
|
|
1198
|
+
let data = useDeferredData();
|
|
1199
|
+
|
|
1200
|
+
if (typeof children === "function") {
|
|
1201
|
+
return children(data);
|
|
1202
|
+
}
|
|
1203
|
+
|
|
1204
|
+
return /*#__PURE__*/React.createElement(React.Fragment, null, children);
|
|
1205
|
+
} ///////////////////////////////////////////////////////////////////////////////
|
|
1206
|
+
// UTILS
|
|
1207
|
+
///////////////////////////////////////////////////////////////////////////////
|
|
1208
|
+
|
|
1209
|
+
/**
|
|
1210
|
+
* Creates a route config from a React "children" object, which is usually
|
|
1211
|
+
* either a `<Route>` element or an array of them. Used internally by
|
|
1212
|
+
* `<Routes>` to create a route config from its children.
|
|
1213
|
+
*
|
|
1214
|
+
* @see https://reactrouter.com/docs/en/v6/utils/create-routes-from-children
|
|
1215
|
+
*/
|
|
1216
|
+
|
|
1217
|
+
|
|
1218
|
+
function createRoutesFromChildren(children, parentPath) {
|
|
1219
|
+
if (parentPath === void 0) {
|
|
1220
|
+
parentPath = [];
|
|
1221
|
+
}
|
|
1222
|
+
|
|
1223
|
+
let routes = [];
|
|
1224
|
+
React.Children.forEach(children, (element, index) => {
|
|
1225
|
+
if (! /*#__PURE__*/React.isValidElement(element)) {
|
|
1226
|
+
// Ignore non-elements. This allows people to more easily inline
|
|
1227
|
+
// conditionals in their route config.
|
|
1228
|
+
return;
|
|
1229
|
+
}
|
|
1230
|
+
|
|
1231
|
+
if (element.type === React.Fragment) {
|
|
1232
|
+
// Transparently support React.Fragment and its children.
|
|
1233
|
+
routes.push.apply(routes, createRoutesFromChildren(element.props.children, parentPath));
|
|
1234
|
+
return;
|
|
1235
|
+
}
|
|
1236
|
+
|
|
1237
|
+
!(element.type === Route) ? process.env.NODE_ENV !== "production" ? invariant(false, "[" + (typeof element.type === "string" ? element.type : element.type.name) + "] is not a <Route> component. All component children of <Routes> must be a <Route> or <React.Fragment>") : invariant(false) : void 0;
|
|
1238
|
+
let treePath = [...parentPath, index];
|
|
1239
|
+
let route = {
|
|
1240
|
+
id: element.props.id || treePath.join("-"),
|
|
1241
|
+
caseSensitive: element.props.caseSensitive,
|
|
1242
|
+
element: element.props.element,
|
|
1243
|
+
index: element.props.index,
|
|
1244
|
+
path: element.props.path,
|
|
1245
|
+
loader: element.props.loader,
|
|
1246
|
+
action: element.props.action,
|
|
1247
|
+
errorElement: element.props.errorElement,
|
|
1248
|
+
shouldRevalidate: element.props.shouldRevalidate,
|
|
1249
|
+
handle: element.props.handle
|
|
1250
|
+
};
|
|
1251
|
+
|
|
1252
|
+
if (element.props.children) {
|
|
1253
|
+
route.children = createRoutesFromChildren(element.props.children, treePath);
|
|
1254
|
+
}
|
|
1255
|
+
|
|
1256
|
+
routes.push(route);
|
|
1257
|
+
});
|
|
1258
|
+
return routes;
|
|
1259
|
+
}
|
|
1260
|
+
/**
|
|
1261
|
+
* Renders the result of `matchRoutes()` into a React element.
|
|
1262
|
+
*/
|
|
1263
|
+
|
|
1264
|
+
function renderMatches(matches) {
|
|
1265
|
+
return _renderMatches(matches);
|
|
1266
|
+
}
|
|
1267
|
+
|
|
1268
|
+
export { DataMemoryRouter, Deferred, MemoryRouter, Navigate, Outlet, Route, Router, Routes, DataRouter as UNSAFE_DataRouter, DataRouterContext as UNSAFE_DataRouterContext, DataRouterProvider as UNSAFE_DataRouterProvider, DataRouterStateContext as UNSAFE_DataRouterStateContext, DataStaticRouterContext as UNSAFE_DataStaticRouterContext, LocationContext as UNSAFE_LocationContext, NavigationContext as UNSAFE_NavigationContext, RouteContext as UNSAFE_RouteContext, createRoutesFromChildren, renderMatches, useActionData, useDeferredData, useHref, useInRouterContext, useLoaderData, useLocation, useMatch, useMatches, useNavigate, useNavigation, useNavigationType, useOutlet, useOutletContext, useParams, useResolvedPath, useRevalidator, useRouteError, useRouteLoaderData, useRoutes };
|
|
1269
|
+
//# sourceMappingURL=index.js.map
|