clear-react-router 2.2.2 → 2.2.3
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/dist/hooks/useAction.d.ts +1 -1
- package/dist/hooks/useSubmitAction.d.ts +1 -2
- package/dist/index.js +9 -9
- package/dist/types.d.ts +2 -2
- package/package.json +1 -1
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
|
```
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
export declare const useAction: (action: string, options?: import("../types").Options) => (arg:
|
|
1
|
+
export declare const useAction: (action: string, options?: import("../types").Options) => (arg: Record<string, unknown>) => Promise<{
|
|
2
2
|
data: unknown;
|
|
3
3
|
error: Error | null;
|
|
4
4
|
}>;
|
|
@@ -3,10 +3,9 @@ type Options = {
|
|
|
3
3
|
onSuccess?(arg: unknown): void;
|
|
4
4
|
onError?(arg: unknown): void;
|
|
5
5
|
autoReset?: boolean;
|
|
6
|
-
withBeforeLoad?: boolean;
|
|
7
6
|
};
|
|
8
7
|
export declare const useSubmitAction: (action: string, options?: Options) => {
|
|
9
|
-
submit: (
|
|
8
|
+
submit: (data: Record<string, unknown>) => Promise<{
|
|
10
9
|
data: unknown;
|
|
11
10
|
error: Error | null;
|
|
12
11
|
}>;
|
package/dist/index.js
CHANGED
|
@@ -47,7 +47,7 @@ var EMPTY_LOADER_STATE = {};
|
|
|
47
47
|
var WINDOW_TOP = "__window_top__";
|
|
48
48
|
var WINDOW_LEFT = "__window_left__";
|
|
49
49
|
//#endregion
|
|
50
|
-
//#region \0@oxc-project+runtime@0.
|
|
50
|
+
//#region \0@oxc-project+runtime@0.132.0/helpers/typeof.js
|
|
51
51
|
function _typeof(o) {
|
|
52
52
|
"@babel/helpers - typeof";
|
|
53
53
|
return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function(o) {
|
|
@@ -57,7 +57,7 @@ function _typeof(o) {
|
|
|
57
57
|
}, _typeof(o);
|
|
58
58
|
}
|
|
59
59
|
//#endregion
|
|
60
|
-
//#region \0@oxc-project+runtime@0.
|
|
60
|
+
//#region \0@oxc-project+runtime@0.132.0/helpers/toPrimitive.js
|
|
61
61
|
function toPrimitive(t, r) {
|
|
62
62
|
if ("object" != _typeof(t) || !t) return t;
|
|
63
63
|
var e = t[Symbol.toPrimitive];
|
|
@@ -69,13 +69,13 @@ function toPrimitive(t, r) {
|
|
|
69
69
|
return ("string" === r ? String : Number)(t);
|
|
70
70
|
}
|
|
71
71
|
//#endregion
|
|
72
|
-
//#region \0@oxc-project+runtime@0.
|
|
72
|
+
//#region \0@oxc-project+runtime@0.132.0/helpers/toPropertyKey.js
|
|
73
73
|
function toPropertyKey(t) {
|
|
74
74
|
var i = toPrimitive(t, "string");
|
|
75
75
|
return "symbol" == _typeof(i) ? i : i + "";
|
|
76
76
|
}
|
|
77
77
|
//#endregion
|
|
78
|
-
//#region \0@oxc-project+runtime@0.
|
|
78
|
+
//#region \0@oxc-project+runtime@0.132.0/helpers/defineProperty.js
|
|
79
79
|
function _defineProperty(e, r, t) {
|
|
80
80
|
return (r = toPropertyKey(r)) in e ? Object.defineProperty(e, r, {
|
|
81
81
|
value: t,
|
|
@@ -644,9 +644,9 @@ var createRouterInstance = () => {
|
|
|
644
644
|
},
|
|
645
645
|
useAction: (action, options = {}) => {
|
|
646
646
|
const currentAction = getCurrentAction(action);
|
|
647
|
-
return async (
|
|
647
|
+
return async (input) => {
|
|
648
648
|
try {
|
|
649
|
-
const data = await currentAction(
|
|
649
|
+
const data = await currentAction(input);
|
|
650
650
|
await invalidate();
|
|
651
651
|
options.onSuccess?.(data);
|
|
652
652
|
return {
|
|
@@ -1105,9 +1105,9 @@ var useSubmitAction = (action, options) => {
|
|
|
1105
1105
|
const [data, setData] = useState();
|
|
1106
1106
|
const [error, setError] = useState(null);
|
|
1107
1107
|
const [isSubmitting, setIsSubmitting] = useState(false);
|
|
1108
|
-
const submit = useCallback(async (
|
|
1108
|
+
const submit = useCallback(async (data) => {
|
|
1109
1109
|
setIsSubmitting(true);
|
|
1110
|
-
const result = await currentAction(
|
|
1110
|
+
const result = await currentAction(data);
|
|
1111
1111
|
setIsSubmitting(false);
|
|
1112
1112
|
setData(result.data);
|
|
1113
1113
|
setError(result.error);
|
|
@@ -1118,7 +1118,7 @@ var useSubmitAction = (action, options) => {
|
|
|
1118
1118
|
onSubmit: useCallback(async (evt) => {
|
|
1119
1119
|
evt.preventDefault();
|
|
1120
1120
|
const target = evt.target;
|
|
1121
|
-
const { error } = await submit(new FormData(target));
|
|
1121
|
+
const { error } = await submit(Object.fromEntries(new FormData(target)));
|
|
1122
1122
|
if (!(options?.autoReset === false) && !error) target.reset();
|
|
1123
1123
|
}, [options?.autoReset, submit]),
|
|
1124
1124
|
data,
|
package/dist/types.d.ts
CHANGED
|
@@ -43,7 +43,7 @@ export type ClientRouteItem = {
|
|
|
43
43
|
params: Record<string, string>;
|
|
44
44
|
searchParams: Record<string, string>;
|
|
45
45
|
}) => Promise<void> | void;
|
|
46
|
-
actions?: (arg: LoaderArgs) => Record<string, (arg:
|
|
46
|
+
actions?: (arg: LoaderArgs) => Record<string, (arg: Record<string, unknown>) => Promise<unknown> | Promise<void> | void | unknown>;
|
|
47
47
|
};
|
|
48
48
|
export type RouteItem = ClientRouteItem & {
|
|
49
49
|
element: RenderElement;
|
|
@@ -142,7 +142,7 @@ export type RouterType = {
|
|
|
142
142
|
useLoaderState<T>(): LoaderState<T>;
|
|
143
143
|
useParams<T>(): T;
|
|
144
144
|
useNavigate(): (arg: NavigationLocation | string | -1) => Promise<void>;
|
|
145
|
-
useAction(action: string, options?: Options): (arg:
|
|
145
|
+
useAction(action: string, options?: Options): (arg: Record<string, unknown>) => Promise<{
|
|
146
146
|
data: unknown;
|
|
147
147
|
error: Error | null;
|
|
148
148
|
}>;
|