clear-react-router 2.1.3 → 2.1.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 CHANGED
@@ -1,3 +1,5 @@
1
+ [![npm version](https://badge.fury.io/js/clear-react-router.svg)](https://www.npmjs.com/package/clear-react-router)
2
+
1
3
  # Clear Router
2
4
 
3
5
  A lightweight, type-safe routing library for client side React applications with nested routes, data loading, navigation blocking, prefetching, and route actions.
@@ -414,12 +416,15 @@ actions?: ({ context, params, invalidate, setContext }) => ({
414
416
 
415
417
  #### Arguments
416
418
 
417
- | Property | Type | Description |
418
- | ------------ | --------------------------------------------------- | -------------------------------------------------------------------------------------------- |
419
- | `context` | `Record<string, unknown>` | Current router context. |
420
- | `params` | `Record<string, string>` | Route parameters. |
421
- | `invalidate` | `(path?: string) => Promise<void>` | Invalidates the current route or a specific route, re-running its `beforeLoad` and `loader`. |
422
- | `setContext` | `Dispatch<SetStateAction<Record<string, unknown>>>` | Updates the router context. |
419
+ ```ts
420
+ {
421
+ params: Record<string, string>; // Route parameters
422
+ context: Record<string, unknown>; // Router context
423
+ setContext: Dispatch<SetStateAction<Record<string, unknown>>>; // Updates the router context
424
+ searchParams: Record<string, string>; // URL search parameters
425
+ location: Location; // Route location
426
+ }
427
+ ```
423
428
 
424
429
  #### Returns
425
430
 
@@ -427,9 +432,7 @@ A record where each key is an action name and each value is a function accepting
427
432
 
428
433
  ## useSubmitAction()
429
434
 
430
- `useSubmitAction` wraps a route's `action` with submission state (`isSubmitting`, `data`, `error`) and
431
- gives you two ways to trigger it — a ready-made `onSubmit` for native forms, or a raw `submit(formData)`
432
- you can call from any form library.
435
+ `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(formData)` you can call from any form library.
433
436
 
434
437
  ```tsx
435
438
  import { useSubmitAction } from 'clear-react-router';
package/dist/index.js CHANGED
@@ -537,17 +537,19 @@ var createRouterInstance = () => {
537
537
  const navigate = createNavigate(routerState, revalidateCache);
538
538
  const prefetch = createPrefetch(routerState, revalidateCache);
539
539
  const useGetCurrentAction = (actionKey) => {
540
- const { routeItem } = routerState.routeItemDataState.getState();
540
+ const { routeItem, location } = routerState.routeItemDataState.getState();
541
+ if (!routeItem) throw new Error("Route not found");
542
+ if (!routeItem.actions) throw new Error("Route action creator not found");
541
543
  const context = routerState.contextState.getState();
542
544
  const setContext = routerState.contextState.setState;
543
545
  const params = getParamsObject();
544
- if (!routeItem) throw new Error("Route not found");
545
- if (!routeItem.actions) throw new Error("Route action creator not found");
546
+ const searchParams = Object.fromEntries(new URLSearchParams(location.search).entries());
546
547
  const action = routeItem.actions({
547
548
  context,
548
549
  setContext,
549
550
  params,
550
- invalidate
551
+ location,
552
+ searchParams
551
553
  })[actionKey];
552
554
  if (!action) throw new Error(`Action "${actionKey}" not found`);
553
555
  return action;
package/dist/types.d.ts CHANGED
@@ -9,25 +9,22 @@ export type LazyComponent = {
9
9
  }>;
10
10
  };
11
11
  export type RenderElement = (() => ReactElement) | ReactElement;
12
- export type BeforeLoad = (arg: {
12
+ type LoaderArgs = {
13
13
  context: Record<string, unknown>;
14
- redirect: (arg: Location | string) => Promise<void>;
15
14
  params: Record<string, string>;
16
15
  setContext: Dispatch<SetStateAction<Record<string, unknown>>>;
17
16
  searchParams: Record<string, string>;
18
17
  location: Location;
18
+ };
19
+ export type BeforeLoad = (arg: LoaderArgs & {
20
+ redirect: (arg: Location | string) => Promise<void>;
19
21
  }) => Promise<unknown> | undefined | void;
20
22
  export type ScrollRestorationBehavior = 'auto' | 'smooth' | 'instant';
21
23
  export type ClientRouteItem = {
22
24
  path: string;
23
25
  element: RenderElement | LazyComponent;
24
- loader?(arg: {
25
- params: Record<string, string>;
26
- context: Record<string, unknown>;
27
- setContext: Dispatch<SetStateAction<Record<string, unknown>>>;
28
- searchParams: Record<string, string>;
26
+ loader?(arg: LoaderArgs & {
29
27
  signal: AbortSignal;
30
- location: Location;
31
28
  }): Promise<unknown>;
32
29
  loaderFallback?: RenderElement;
33
30
  errorElement?: RenderElement;
@@ -46,12 +43,7 @@ export type ClientRouteItem = {
46
43
  params: Record<string, string>;
47
44
  setContext: Dispatch<SetStateAction<Record<string, unknown>>>;
48
45
  }) => Promise<void>;
49
- actions?: (arg: {
50
- context: Record<string, unknown>;
51
- params: Record<string, string>;
52
- invalidate: (path?: string) => Promise<InvalidateResult[]>;
53
- setContext: Dispatch<SetStateAction<Record<string, unknown>>>;
54
- }) => Record<string, (arg: FormData) => Promise<unknown> | Promise<void> | void | unknown>;
46
+ actions?: (arg: LoaderArgs) => Record<string, (arg: FormData) => Promise<unknown> | Promise<void> | void | unknown>;
55
47
  };
56
48
  export type RouteItem = ClientRouteItem & {
57
49
  element: RenderElement;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "clear-react-router",
3
- "version": "2.1.3",
3
+ "version": "2.1.4",
4
4
  "description": "A lightweight, type-safe routing library for React applications",
5
5
  "author": "Andrew Bubnov",
6
6
  "scripts": {