clear-react-router 2.2.2 → 2.2.4
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 +19 -32
- package/package.json +1 -1
- package/dist/components/Link.d.ts +0 -23
- package/dist/components/Router.d.ts +0 -2
- package/dist/components/Spinner.d.ts +0 -1
- package/dist/config/routerConfig.d.ts +0 -18
- package/dist/constants.d.ts +0 -5
- package/dist/create.d.ts +0 -10
- package/dist/creators/createRouter.d.ts +0 -2
- package/dist/creators/createRouterInstance.d.ts +0 -2
- package/dist/hooks/useAction.d.ts +0 -4
- package/dist/hooks/useApplyCustomAnimation.d.ts +0 -1
- package/dist/hooks/useBlocker.d.ts +0 -13
- package/dist/hooks/useInvalidate.d.ts +0 -1
- package/dist/hooks/useIsDataLoading.d.ts +0 -1
- package/dist/hooks/useIsRoutePending.d.ts +0 -1
- package/dist/hooks/useLoaderState.d.ts +0 -1
- package/dist/hooks/useLocation.d.ts +0 -1
- package/dist/hooks/useNavigate.d.ts +0 -1
- package/dist/hooks/useNavigation.d.ts +0 -1
- package/dist/hooks/useParams.d.ts +0 -1
- package/dist/hooks/usePreserveScroll.d.ts +0 -2
- package/dist/hooks/useReload.d.ts +0 -1
- package/dist/hooks/useRouterContext.d.ts +0 -4
- package/dist/hooks/useSearch.d.ts +0 -1
- package/dist/hooks/useSearchParams.d.ts +0 -10
- package/dist/hooks/useSetInitialContext.d.ts +0 -2
- package/dist/hooks/useSetRouterConfig.d.ts +0 -2
- package/dist/hooks/useSubmitAction.d.ts +0 -18
- package/dist/index.d.ts +0 -16
- package/dist/index.js +0 -1168
- package/dist/instance.d.ts +0 -1
- package/dist/runtime/invalidate.d.ts +0 -2
- package/dist/runtime/navigate.d.ts +0 -2
- package/dist/runtime/prefetch.d.ts +0 -2
- package/dist/runtime/revalidateCache.d.ts +0 -2
- package/dist/types.d.ts +0 -180
- package/dist/utils/commitNavigation.d.ts +0 -1
- package/dist/utils/commitState.d.ts +0 -8
- package/dist/utils/createLazyComponent.d.ts +0 -9
- package/dist/utils/findRoute.d.ts +0 -2
- package/dist/utils/isCacheItemFresh.d.ts +0 -2
- package/dist/utils/lazy.d.ts +0 -5
- package/dist/utils/renderElement.d.ts +0 -2
- package/dist/utils/utils.d.ts +0 -18
package/README.md
CHANGED
|
@@ -76,18 +76,18 @@ Normalizes route configuration. Extracts dynamic params, builds nested paths.
|
|
|
76
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
|
-
| `loaderFallback` | `ReactElement \| () => ReactElement` | Loading fallback for the route's `loader`. Overrides the global `defaultLoaderFallback`
|
|
79
|
+
| `loaderFallback` | `ReactElement \| () => ReactElement` | Loading fallback for the route's `loader`. Overrides the global `Router.defaultLoaderFallback` |
|
|
80
80
|
| `retry` | `number \| { count: number; delay: number }` | Overrides the global cache revalidation retry policy for this route |
|
|
81
81
|
| `optimistic` | `boolean \| undefined` | Instant navigation using stale data while fresh data is loaded in the background |
|
|
82
|
-
| `errorElement` | `ReactElement \| () => ReactElement` | Error fallback for the route. Overrides the global `defaultErrorElement`
|
|
82
|
+
| `errorElement` | `ReactElement \| () => ReactElement` | Error fallback for the route. Overrides the global `Router.defaultErrorElement` |
|
|
83
83
|
| `staleTime` | `number \| undefined` | Time in milliseconds before cached loader data is considered stale. Overrides Router.defaultStaleTime. If neither value is provided, cached data never expires |
|
|
84
84
|
| `gcTime` | `number \| undefined` | How long, in milliseconds, an unused cache entry is kept in memory after you navigate away, before it's garbage-collected |
|
|
85
|
-
| `actions` | `({ params, context,
|
|
85
|
+
| `actions` | `({ params, context, searchParams, setContext, location }) => Record<string, (data: Record<string, unknown>) => unknown \| Promise<unknown>>` | Defines route actions for data mutations. |
|
|
86
86
|
| `pollingInterval` | `number \| undefined` | Polling interval (in milliseconds) for automatically revalidating data while the route is active |
|
|
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
|
-
`beforeLoad` and `
|
|
90
|
+
`beforeLoad`, `loader` and `actions` receive:
|
|
91
91
|
|
|
92
92
|
```ts
|
|
93
93
|
{
|
|
@@ -405,14 +405,9 @@ Defines route-specific actions for handling data mutations such as creating, upd
|
|
|
405
405
|
Actions are available through the `useSubmitAction` and `useAction` hooks. After a successful action, the current route is automatically invalidated, causing `loader` to run again in the background.
|
|
406
406
|
|
|
407
407
|
```tsx
|
|
408
|
-
actions?: ({
|
|
409
|
-
save: async (
|
|
410
|
-
|
|
411
|
-
},
|
|
412
|
-
|
|
413
|
-
remove: async () => {
|
|
414
|
-
await api.deletePost(params.id);
|
|
415
|
-
},
|
|
408
|
+
actions?: ({ params, context, searchParams, setContext, location }) => ({
|
|
409
|
+
save: async (data: Record<string, unknown>) => await api.updatePost(params.id, data),
|
|
410
|
+
remove: async () => await api.deletePost(params.id),
|
|
416
411
|
})
|
|
417
412
|
```
|
|
418
413
|
|
|
@@ -430,11 +425,11 @@ actions?: ({ context, params, invalidate, setContext }) => ({
|
|
|
430
425
|
|
|
431
426
|
#### Returns
|
|
432
427
|
|
|
433
|
-
A record where each key is an action name and each value is a function accepting
|
|
428
|
+
A record where each key is an action name and each value is a function accepting an object data argument.
|
|
434
429
|
|
|
435
430
|
## useSubmitAction()
|
|
436
431
|
|
|
437
|
-
`useSubmitAction` wraps a route's `action` with submission state (`isSubmitting`, `data`, `error`) and gives you two ways to trigger it — a ready-made `onSubmit` for native forms, or a raw `submit(
|
|
432
|
+
`useSubmitAction` wraps a route's `action` with submission state (`isSubmitting`, `data`, `error`) and gives you two ways to trigger it — a ready-made `onSubmit` for native forms, or a raw `submit(data)` you can call from any form library.
|
|
438
433
|
|
|
439
434
|
```tsx
|
|
440
435
|
import { useSubmitAction } from 'clear-react-router';
|
|
@@ -452,8 +447,7 @@ const CreateUserForm = () => {
|
|
|
452
447
|
};
|
|
453
448
|
```
|
|
454
449
|
|
|
455
|
-
`submit(
|
|
456
|
-
validated values — not just native `<form>` submissions:
|
|
450
|
+
`submit(data: Record<string, unknown>)` accepts a plain JS object, so it works with any form library that gives you validated values — not just native `<form>` submissions:
|
|
457
451
|
|
|
458
452
|
```tsx
|
|
459
453
|
import { useForm } from 'react-hook-form';
|
|
@@ -461,13 +455,7 @@ import { useForm } from 'react-hook-form';
|
|
|
461
455
|
const { register, handleSubmit } = useForm<FormValues>();
|
|
462
456
|
const { submit, isSubmitting } = useSubmitAction('createUser');
|
|
463
457
|
|
|
464
|
-
|
|
465
|
-
const formData = new FormData();
|
|
466
|
-
Object.entries(values).forEach(([key, value]) => formData.append(key, String(value)));
|
|
467
|
-
return submit(formData);
|
|
468
|
-
};
|
|
469
|
-
|
|
470
|
-
<form onSubmit={handleSubmit(onValid)}>
|
|
458
|
+
<form onSubmit={handleSubmit(submit)}>
|
|
471
459
|
<input {...register('email')} disabled={isSubmitting} />
|
|
472
460
|
</form>
|
|
473
461
|
```
|
|
@@ -480,18 +468,22 @@ Arguments:
|
|
|
480
468
|
| `options.onSuccess` | `(data: unknown) => void \| undefined` | `undefined` | Called after a successful submission |
|
|
481
469
|
| `options.onError` | `(error: unknown) => void \| undefined` | `undefined` | Called if the action throws |
|
|
482
470
|
| `options.autoReset` | `boolean \| undefined` | `true` | Reset the form element after a successful native submission (`onSubmit` only — has no effect on `submit`) |
|
|
483
|
-
| `options.withBeforeLoad` | `boolean \| undefined` | `false` | Whether to run the `beforeLoad` hook on a successful action submit as well |
|
|
484
471
|
|
|
485
472
|
Return value:
|
|
486
473
|
|
|
487
474
|
| Field | Type | Description |
|
|
488
475
|
|---|---|---|
|
|
489
|
-
| `submit` | `(
|
|
476
|
+
| `submit` | `(data: Record<string, unknown>) => Promise<{ data: unknown; error: Error \| null }>` | Runs the action directly, from any form data source |
|
|
490
477
|
| `onSubmit` | `(evt: SubmitEvent<HTMLFormElement>) => Promise<void>` | Ready-made handler for a native `<form onSubmit={...}>` |
|
|
491
478
|
| `data` | `unknown` | Result of the last successful submission |
|
|
492
479
|
| `error` | `Error \| null` | Error from the last failed submission |
|
|
493
480
|
| `isSubmitting` | `boolean` | `true` while the action is in flight |
|
|
494
481
|
|
|
482
|
+
> The native `onSubmit` handler converts the form via `Object.fromEntries(new FormData(...))` —
|
|
483
|
+
> if your form has multiple fields sharing the same `name` (checkbox groups, `<select multiple>`),
|
|
484
|
+
> only the last value survives. Use `submit(data)` with your own data source (e.g. React Hook Form)
|
|
485
|
+
> if you need to preserve multiple values per field.
|
|
486
|
+
|
|
495
487
|
## useAction()
|
|
496
488
|
|
|
497
489
|
`useAction` provides direct access to a route action.
|
|
@@ -504,17 +496,12 @@ Return value:
|
|
|
504
496
|
| `options` | `Options` | Optional callbacks invoked after the action succeeds or fails. |
|
|
505
497
|
|
|
506
498
|
```ts
|
|
507
|
-
type Options = { onSuccess?: (args: unknown) => void; onError?: (args: unknown) => void }
|
|
499
|
+
type Options = { onSuccess?: (args: unknown) => void; onError?: (args: unknown) => void };
|
|
508
500
|
```
|
|
509
501
|
|
|
510
502
|
```tsx
|
|
511
503
|
const save = useAction('save');
|
|
512
|
-
|
|
513
|
-
const handleClick = async () => {
|
|
514
|
-
const data = new FormData();
|
|
515
|
-
data.append('title', 'Hello');
|
|
516
|
-
await save(data);
|
|
517
|
-
};
|
|
504
|
+
const handleClick = async () => await save({ data: 'Hello world!' });
|
|
518
505
|
|
|
519
506
|
<button onClick={handleClick}>Save</button>
|
|
520
507
|
```
|
package/package.json
CHANGED
|
@@ -1,23 +0,0 @@
|
|
|
1
|
-
import { type CSSProperties, ReactNode, ReactElement } from 'react';
|
|
2
|
-
import { ElementProps, RouterProps, SearchObject } from '../types';
|
|
3
|
-
type ElementState = {
|
|
4
|
-
isActive: boolean;
|
|
5
|
-
isPending: boolean;
|
|
6
|
-
};
|
|
7
|
-
type LinkProps<T extends HTMLElement = HTMLAnchorElement> = {
|
|
8
|
-
to: string;
|
|
9
|
-
search?: string | SearchObject;
|
|
10
|
-
state?: unknown;
|
|
11
|
-
children?: ReactNode;
|
|
12
|
-
as?: (props: ElementProps<T>, state: ElementState) => ReactElement;
|
|
13
|
-
prefetch?: RouterProps['defaultPrefetch'];
|
|
14
|
-
hoverPrefetchDelay?: number;
|
|
15
|
-
className?: string | ((arg: ElementState) => string);
|
|
16
|
-
activeClassName?: string;
|
|
17
|
-
pendingClassName?: string;
|
|
18
|
-
beforeNavigate?(): Promise<void>;
|
|
19
|
-
style?: CSSProperties | ((arg: ElementState) => CSSProperties);
|
|
20
|
-
exact?: boolean;
|
|
21
|
-
};
|
|
22
|
-
export declare const Link: <T extends HTMLElement = HTMLAnchorElement>({ children, to, search, state, as, prefetch: linkPrefetch, hoverPrefetchDelay, className, style, beforeNavigate, exact, activeClassName, pendingClassName, }: LinkProps<T>) => ReactElement<unknown, string | import("react").JSXElementConstructor<any>>;
|
|
23
|
-
export {};
|
|
@@ -1,2 +0,0 @@
|
|
|
1
|
-
import { RouterProps } from '../types';
|
|
2
|
-
export declare const Router: ({ routes, defaultBeforeLoad, defaultAfterLoad, animationDuration, defaultLoaderFallback, defaultErrorElement, defaultRetry, defaultStaleTime, context: initialContext, isAnimated, optimisticSpinner, defaultMinLoaderDuration, revalidateOnFocus, revalidateOnReconnect, maxCacheSize, defaultPrefetch, defaultHoverPrefetchDelay, defaultScrollRestorationBehavior, errorBoundary: ErrorBoundary, }: RouterProps) => import("react/jsx-runtime").JSX.Element | null;
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export declare const Spinner: () => import("react/jsx-runtime").JSX.Element;
|
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
import { ClientRouteItem, RouterProps } from '../types';
|
|
2
|
-
declare class RouterConfig {
|
|
3
|
-
routes: RouterProps['routes'];
|
|
4
|
-
maxCacheSize: number;
|
|
5
|
-
defaultPrefetch: RouterProps['defaultPrefetch'];
|
|
6
|
-
isAnimated: RouterProps['isAnimated'];
|
|
7
|
-
revalidateOnFocus: boolean;
|
|
8
|
-
revalidateOnReconnect: boolean;
|
|
9
|
-
defaultHoverPrefetchDelay: number;
|
|
10
|
-
defaultBeforeLoad?: ClientRouteItem['beforeLoad'];
|
|
11
|
-
defaultAfterLoad?: ClientRouteItem['afterLoad'];
|
|
12
|
-
defaultRetry?: RouterProps['defaultRetry'];
|
|
13
|
-
defaultStaleTime?: RouterProps['defaultStaleTime'];
|
|
14
|
-
defaultMinLoaderDuration?: RouterProps['defaultMinLoaderDuration'];
|
|
15
|
-
configure(config: Partial<RouterConfig>): void;
|
|
16
|
-
}
|
|
17
|
-
export declare const routerConfig: RouterConfig;
|
|
18
|
-
export {};
|
package/dist/constants.d.ts
DELETED
package/dist/create.d.ts
DELETED
|
@@ -1,10 +0,0 @@
|
|
|
1
|
-
type SetStateAction<T> = ((prevState: T) => T) | T;
|
|
2
|
-
type Listener<T> = (state: T, prevState: T) => void;
|
|
3
|
-
export type Store<T> = {
|
|
4
|
-
subscribe: (listener: Listener<T>) => () => void;
|
|
5
|
-
getState: () => T;
|
|
6
|
-
setState: (action: SetStateAction<T>) => void;
|
|
7
|
-
};
|
|
8
|
-
export declare const create: <T>(initialState: T) => Store<T>;
|
|
9
|
-
export declare const useGlobalState: <T>({ subscribe, getState, setState }: Store<T>) => readonly [T, (action: SetStateAction<T>) => void];
|
|
10
|
-
export {};
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export declare const useApplyCustomAnimation: (animationDuration?: number) => void;
|
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
import { BlockerState, Location } from '../types';
|
|
2
|
-
type UseBlockerReturnValue = {
|
|
3
|
-
state: BlockerState;
|
|
4
|
-
process(): void;
|
|
5
|
-
reset(): void;
|
|
6
|
-
};
|
|
7
|
-
type BlockerCallback = {
|
|
8
|
-
location: Location;
|
|
9
|
-
nextLocation: Location | null;
|
|
10
|
-
context: Record<string, unknown>;
|
|
11
|
-
};
|
|
12
|
-
export declare const useBlocker: (blockerFn: (args: BlockerCallback) => boolean) => UseBlockerReturnValue;
|
|
13
|
-
export {};
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export declare const useInvalidate: () => (pathList?: string | string[], options?: import("../types").InvalidateOptions) => Promise<import("../types").InvalidateResult[]>;
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export declare const useIsDataLoading: () => boolean;
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export declare const useIsRoutePending: (routePath: string) => boolean;
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export declare const useLoaderState: <T>() => import("../types").LoaderState<T>;
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export declare const useLocation: () => import("..").Location;
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export declare const useNavigate: () => (arg: import("..").NavigationLocation | string | -1) => Promise<void>;
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export declare const useNavigation: () => void;
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export declare const useParams: <T>() => T;
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export declare const useReload: () => void;
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export declare const useSearch: () => string;
|
|
@@ -1,10 +0,0 @@
|
|
|
1
|
-
type UseSearchParamsReturn = {
|
|
2
|
-
searchParams: URLSearchParams;
|
|
3
|
-
getSearchParams(arg: string): string | string[];
|
|
4
|
-
setSearchParams: {
|
|
5
|
-
(param: string, value: string | string[]): void;
|
|
6
|
-
(param: (prevState: URLSearchParams) => URLSearchParams): void;
|
|
7
|
-
};
|
|
8
|
-
};
|
|
9
|
-
export declare const useSearchParams: () => UseSearchParamsReturn;
|
|
10
|
-
export {};
|
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
import { SubmitEvent } from 'react';
|
|
2
|
-
type Options = {
|
|
3
|
-
onSuccess?(arg: unknown): void;
|
|
4
|
-
onError?(arg: unknown): void;
|
|
5
|
-
autoReset?: boolean;
|
|
6
|
-
withBeforeLoad?: boolean;
|
|
7
|
-
};
|
|
8
|
-
export declare const useSubmitAction: (action: string, options?: Options) => {
|
|
9
|
-
submit: (formData: FormData) => Promise<{
|
|
10
|
-
data: unknown;
|
|
11
|
-
error: Error | null;
|
|
12
|
-
}>;
|
|
13
|
-
onSubmit: (evt: SubmitEvent<HTMLFormElement>) => Promise<void>;
|
|
14
|
-
data: unknown;
|
|
15
|
-
error: Error | null;
|
|
16
|
-
isSubmitting: boolean;
|
|
17
|
-
};
|
|
18
|
-
export {};
|
package/dist/index.d.ts
DELETED
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
export { Router } from './components/Router';
|
|
2
|
-
export { Link } from './components/Link';
|
|
3
|
-
export { useNavigate } from './hooks/useNavigate';
|
|
4
|
-
export { useParams } from './hooks/useParams';
|
|
5
|
-
export { useLocation } from './hooks/useLocation';
|
|
6
|
-
export { useLoaderState } from './hooks/useLoaderState';
|
|
7
|
-
export { useInvalidate } from './hooks/useInvalidate';
|
|
8
|
-
export { useBlocker } from './hooks/useBlocker';
|
|
9
|
-
export { useAction } from './hooks/useAction';
|
|
10
|
-
export { useIsDataLoading } from './hooks/useIsDataLoading';
|
|
11
|
-
export { useRouterContext } from './hooks/useRouterContext';
|
|
12
|
-
export { useSearchParams } from './hooks/useSearchParams';
|
|
13
|
-
export { useSubmitAction } from './hooks/useSubmitAction';
|
|
14
|
-
export { createRouter } from './creators/createRouter';
|
|
15
|
-
export { lazy } from './utils/lazy';
|
|
16
|
-
export type { RouteItem, BlockerState, Location, RouterProps, ElementProps, NavigationLocation } from './types';
|