clear-react-router 2.2.1 → 2.2.2
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/README.md +37 -6
- package/dist/index.js +52 -31
- package/dist/runtime/prefetch.d.ts +1 -1
- package/dist/types.d.ts +3 -2
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -73,7 +73,7 @@ Normalizes route configuration. Extracts dynamic params, builds nested paths.
|
|
|
73
73
|
| `element` | `ReactElement \| () => ReactElement \| LazyComponent` | Component to render |
|
|
74
74
|
| `beforeLoad` | `({ params, context, redirect, setContext, location }) => Promise<unknown> \| undefined \| void` | Runs before every route navigation. Auth checks and redirects. Can update context via `setContext`. `redirect` is provided by the router |
|
|
75
75
|
| `loader` | `({ params, context, setContext, searchParams, signal }) => Promise<unknown>` | Fetch data using route params, search params, abort controller signal and context. Can update context via `setContext` |
|
|
76
|
-
| `afterLoad` | `({ params, context,
|
|
76
|
+
| `afterLoad` | `({ params, context, searchParams }) => Promise<void> \| void` | Runs after a successful navigation once the route has finished loading. Analytics, side effects after data is loaded. Can update context via `setContext` |
|
|
77
77
|
| `minLoaderDuration` | `number \| undefined` | `undefined` | Minimum time the loader fallback stays visible, to avoid flickering |
|
|
78
78
|
| `fallback` | `ReactElement \| () => ReactElement` | Loading fallback (for lazy loading) |
|
|
79
79
|
| `loaderFallback` | `ReactElement \| () => ReactElement` | Loading fallback for the route's `loader`. Overrides the global `defaultLoaderFallback` set in `Router` |
|
|
@@ -87,8 +87,6 @@ Normalizes route configuration. Extracts dynamic params, builds nested paths.
|
|
|
87
87
|
| `scrollRestoration` | `boolean \| string[] \| undefined` | Restore scroll position when navigating back to this route. `true` restores the window scroll; a string array restores scroll inside specific scrollable elements, matched by their `id` |
|
|
88
88
|
| `scrollRestorationBehavior` | `'auto' \| 'smooth' \| 'instant'` | Scroll restoration behavior |
|
|
89
89
|
|
|
90
|
-
Before load arguments (see [`redirect`](#redirect) for details on programmatic redirects):
|
|
91
|
-
|
|
92
90
|
`beforeLoad` and `loader` both receive:
|
|
93
91
|
|
|
94
92
|
```ts
|
|
@@ -100,13 +98,15 @@ Before load arguments (see [`redirect`](#redirect) for details on programmatic r
|
|
|
100
98
|
location: Location; // Route location
|
|
101
99
|
}
|
|
102
100
|
```
|
|
103
|
-
|
|
101
|
+
see [`Location`](#uselocation) and [`Where you came from`](#where-you-came-from) for type details
|
|
102
|
+
|
|
104
103
|
`beforeLoad` additionally receives:
|
|
105
104
|
```ts
|
|
106
105
|
{
|
|
107
106
|
redirect: (arg: Location | string) => Promise<void>; // Programmatic redirection, see [redirect](#redirect)
|
|
108
107
|
}
|
|
109
108
|
```
|
|
109
|
+
see [`redirect`](#redirect) for details on programmatic redirects
|
|
110
110
|
|
|
111
111
|
`loader` additionally receives:
|
|
112
112
|
```ts
|
|
@@ -544,7 +544,7 @@ const App = () => <Router routes={routes} errorBoundary={ErrorBoundary} />
|
|
|
544
544
|
Returns a function to navigate programmatically. Accepts a string (pathname, optionally with a `?query`), a `NavigationLocation` object, or `-1` to go back.
|
|
545
545
|
|
|
546
546
|
```tsx
|
|
547
|
-
type NavigationLocation = { pathname: string;
|
|
547
|
+
type NavigationLocation = { pathname: string; search?: string | Record<string, string | number | boolean | null | undefined>; state?: unknown }
|
|
548
548
|
|
|
549
549
|
const navigate = useNavigate();
|
|
550
550
|
|
|
@@ -553,7 +553,7 @@ navigate({ pathname: '/user/123', search: { user: 'Jane Doe' }, state: { from: '
|
|
|
553
553
|
navigate(-1); // go back
|
|
554
554
|
```
|
|
555
555
|
|
|
556
|
-
**Note:** Navigation state can be accessed via `useLocation()
|
|
556
|
+
**Note:** Navigation state can be accessed via [`useLocation()`](#uselocation):
|
|
557
557
|
|
|
558
558
|
```tsx
|
|
559
559
|
const navigate = useNavigate();
|
|
@@ -577,8 +577,39 @@ const params = useParams<{ userId: string }>();
|
|
|
577
577
|
|
|
578
578
|
Returns current location `{ pathname, search, state }`.
|
|
579
579
|
```tsx
|
|
580
|
+
type Location = {
|
|
581
|
+
pathname: string;
|
|
582
|
+
search?: string;
|
|
583
|
+
state?: unknown;
|
|
584
|
+
prevLocation?: Omit<Location, 'state' | 'prevLocation'>;
|
|
585
|
+
}
|
|
586
|
+
|
|
580
587
|
const { pathname, search, state } = useLocation();
|
|
581
588
|
```
|
|
589
|
+
### Where you came from
|
|
590
|
+
|
|
591
|
+
Every `location` carries `prevLocation` — the pathname and search of the route you
|
|
592
|
+
navigated away from. Useful for a contextual "Back" link, or for redirect logic in
|
|
593
|
+
`beforeLoad`:
|
|
594
|
+
|
|
595
|
+
```tsx
|
|
596
|
+
const location = useLocation();
|
|
597
|
+
|
|
598
|
+
<Link to={location.prevLocation?.pathname ?? '/'}>← Back</Link>
|
|
599
|
+
```
|
|
600
|
+
|
|
601
|
+
```tsx
|
|
602
|
+
// beforeLoad
|
|
603
|
+
beforeLoad: async ({ location, redirect }) => {
|
|
604
|
+
if (!isAuthenticated && location.prevLocation?.pathname !== '/login') {
|
|
605
|
+
await redirect('/login');
|
|
606
|
+
}
|
|
607
|
+
}
|
|
608
|
+
```
|
|
609
|
+
|
|
610
|
+
> `prevLocation` reflects only the immediately preceding route — it doesn't accumulate
|
|
611
|
+
> a full navigation history, and it's `undefined` on the very first render (nothing to
|
|
612
|
+
> navigate away from yet).
|
|
582
613
|
|
|
583
614
|
### `useLoaderState<T>()`
|
|
584
615
|
|
package/dist/index.js
CHANGED
|
@@ -236,10 +236,6 @@ var createNavigate = (routerState, revalidateCache) => {
|
|
|
236
236
|
return abortController.signal;
|
|
237
237
|
};
|
|
238
238
|
const getPath = (nextLocation) => `${nextLocation.pathname}${nextLocation.search}`;
|
|
239
|
-
const getContext = () => ({
|
|
240
|
-
context: contextState.getState(),
|
|
241
|
-
setContext: contextState.setState
|
|
242
|
-
});
|
|
243
239
|
const routeResolve = (location) => {
|
|
244
240
|
const nextItem = findRoute(location.pathname, true);
|
|
245
241
|
return {
|
|
@@ -250,7 +246,20 @@ var createNavigate = (routerState, revalidateCache) => {
|
|
|
250
246
|
const beforeLoad = async (routeItem, nextLocation) => {
|
|
251
247
|
const { defaultBeforeLoad } = routerConfig;
|
|
252
248
|
const runBeforeLoad = async (loaderFn) => {
|
|
253
|
-
const redirect = async (redirected) =>
|
|
249
|
+
const redirect = async (redirected) => {
|
|
250
|
+
const { location } = routeItemDataState.getState();
|
|
251
|
+
const prevLocation = {
|
|
252
|
+
pathname: location.pathname,
|
|
253
|
+
search: location.search
|
|
254
|
+
};
|
|
255
|
+
return await navigate(typeof redirected === "string" ? {
|
|
256
|
+
pathname: redirected,
|
|
257
|
+
prevLocation
|
|
258
|
+
} : {
|
|
259
|
+
...redirected,
|
|
260
|
+
prevLocation
|
|
261
|
+
});
|
|
262
|
+
};
|
|
254
263
|
try {
|
|
255
264
|
await loaderFn({
|
|
256
265
|
redirect,
|
|
@@ -328,14 +337,14 @@ var createNavigate = (routerState, revalidateCache) => {
|
|
|
328
337
|
};
|
|
329
338
|
const afterLoad = async (routeItem, params) => {
|
|
330
339
|
const { defaultAfterLoad } = routerConfig;
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
340
|
+
const searchParams = Object.fromEntries(new URLSearchParams(location.search).entries());
|
|
341
|
+
const args = {
|
|
342
|
+
context: contextState.getState(),
|
|
343
|
+
params,
|
|
344
|
+
searchParams
|
|
345
|
+
};
|
|
346
|
+
if (routeItem?.afterLoad) await routeItem.afterLoad(args);
|
|
347
|
+
if (defaultAfterLoad) await defaultAfterLoad(args);
|
|
339
348
|
};
|
|
340
349
|
const checkBlocked = (nextLocation) => {
|
|
341
350
|
if (blockerState.getState() === "charged") {
|
|
@@ -385,7 +394,7 @@ var createNavigate = (routerState, revalidateCache) => {
|
|
|
385
394
|
loaderStateValue
|
|
386
395
|
}));
|
|
387
396
|
clearCurrentGcTimeout(nextItem, nextLocation);
|
|
388
|
-
|
|
397
|
+
afterLoad(nextItem, params);
|
|
389
398
|
};
|
|
390
399
|
return navigate;
|
|
391
400
|
};
|
|
@@ -610,24 +619,28 @@ var createRouterInstance = () => {
|
|
|
610
619
|
const { routeItem, location } = routerState.routeItemDataState.getState();
|
|
611
620
|
return getParams(location, routeItem);
|
|
612
621
|
},
|
|
613
|
-
useNavigate: () => {
|
|
622
|
+
useNavigate: () => async (arg) => {
|
|
614
623
|
const { location } = routerState.routeItemDataState.getState();
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
const [pathname, search = ""] = arg.split("?");
|
|
619
|
-
if (pathname !== location.pathname || search !== location.search) await navigate({
|
|
620
|
-
pathname,
|
|
621
|
-
search
|
|
622
|
-
});
|
|
623
|
-
return;
|
|
624
|
-
}
|
|
625
|
-
const search = typeof arg.search === "object" ? formatSearchObject(arg.search) : arg.search;
|
|
626
|
-
await navigate({
|
|
627
|
-
...arg,
|
|
628
|
-
search
|
|
629
|
-
});
|
|
624
|
+
const prevLocation = {
|
|
625
|
+
pathname: location.pathname,
|
|
626
|
+
search: location.search
|
|
630
627
|
};
|
|
628
|
+
if (arg === -1) return history.go(arg);
|
|
629
|
+
if (typeof arg === "string") {
|
|
630
|
+
const [pathname, search = ""] = arg.split("?");
|
|
631
|
+
if (pathname !== location.pathname || search !== location.search) await navigate({
|
|
632
|
+
pathname,
|
|
633
|
+
search,
|
|
634
|
+
prevLocation
|
|
635
|
+
});
|
|
636
|
+
return;
|
|
637
|
+
}
|
|
638
|
+
const search = typeof arg.search === "object" ? formatSearchObject(arg.search) : arg.search;
|
|
639
|
+
await navigate({
|
|
640
|
+
...arg,
|
|
641
|
+
search,
|
|
642
|
+
prevLocation
|
|
643
|
+
});
|
|
631
644
|
},
|
|
632
645
|
useAction: (action, options = {}) => {
|
|
633
646
|
const currentAction = getCurrentAction(action);
|
|
@@ -669,8 +682,16 @@ var useNavigation = () => {
|
|
|
669
682
|
useEffect(() => {
|
|
670
683
|
const handler = async (event) => {
|
|
671
684
|
const newLocation = parseWindowLocation(event.target.location);
|
|
685
|
+
const { location } = routeItemDataState.getState();
|
|
686
|
+
const prevLocation = {
|
|
687
|
+
pathname: location.pathname,
|
|
688
|
+
search: location.search
|
|
689
|
+
};
|
|
672
690
|
if (blockerState.getState() === "charged") history.pushState(null, "", routeItemDataState.getState().location.pathname);
|
|
673
|
-
await navigate(
|
|
691
|
+
await navigate({
|
|
692
|
+
...newLocation,
|
|
693
|
+
prevLocation
|
|
694
|
+
});
|
|
674
695
|
};
|
|
675
696
|
window.addEventListener("popstate", handler);
|
|
676
697
|
return () => window.removeEventListener("popstate", handler);
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import { RevalidateCache, RouterState } from '../types';
|
|
1
|
+
import { RevalidateCache, RouterState, Location } from '../types';
|
|
2
2
|
export declare const createPrefetch: ({ routeItemDataState }: RouterState, revalidateCache: RevalidateCache) => (location: Location) => Promise<void>;
|
package/dist/types.d.ts
CHANGED
|
@@ -41,8 +41,8 @@ export type ClientRouteItem = {
|
|
|
41
41
|
afterLoad?: (arg: {
|
|
42
42
|
context: Record<string, unknown>;
|
|
43
43
|
params: Record<string, string>;
|
|
44
|
-
|
|
45
|
-
}) => Promise<void
|
|
44
|
+
searchParams: Record<string, string>;
|
|
45
|
+
}) => Promise<void> | void;
|
|
46
46
|
actions?: (arg: LoaderArgs) => Record<string, (arg: FormData) => Promise<unknown> | Promise<void> | void | unknown>;
|
|
47
47
|
};
|
|
48
48
|
export type RouteItem = ClientRouteItem & {
|
|
@@ -57,6 +57,7 @@ export type Location = {
|
|
|
57
57
|
pathname: string;
|
|
58
58
|
search?: string;
|
|
59
59
|
state?: unknown;
|
|
60
|
+
prevLocation?: Omit<Location, 'state' | 'prevLocation'>;
|
|
60
61
|
};
|
|
61
62
|
export type BlockerState = 'blocked' | 'unblocked' | 'charged';
|
|
62
63
|
export type RevalidateCacheArgs = {
|