clear-react-router 1.6.4 → 1.6.5
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 +20 -18
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -59,7 +59,7 @@ The root component that provides routing context to the application. Place stati
|
|
|
59
59
|
| `context` | `object` | `{}` | Initial context (user, theme, etc.) |
|
|
60
60
|
| `children` | `ReactNode` | required | App content (must include `<Router />`) |
|
|
61
61
|
|
|
62
|
-
```
|
|
62
|
+
```tsx
|
|
63
63
|
function App() {
|
|
64
64
|
return (
|
|
65
65
|
<RouterProvider routeList={routes}>
|
|
@@ -88,7 +88,7 @@ Renders the current route's component. Must be placed inside `<RouterProvider>`.
|
|
|
88
88
|
| `hoverPrefetchDelay` | `number` | `150` | Delay in milliseconds before prefetching on hover (only for `'hover'` strategy) |
|
|
89
89
|
| `errorBoundary` | `ComponentType<{ children: ReactNode }>` | `undefined` | Custom error boundary component for catching render errors in route components |
|
|
90
90
|
|
|
91
|
-
```
|
|
91
|
+
```tsx
|
|
92
92
|
<RouterProvider routes={routes}>
|
|
93
93
|
<Navbar />
|
|
94
94
|
<Router spinner={false} isAnimated /> {/* disable the spinner */}
|
|
@@ -119,7 +119,7 @@ Component for client-side navigation with prefetch support.
|
|
|
119
119
|
|
|
120
120
|
**Example:**
|
|
121
121
|
|
|
122
|
-
```
|
|
122
|
+
```tsx
|
|
123
123
|
import { RouterProvider, Router, Link } from 'clear-react-router';
|
|
124
124
|
|
|
125
125
|
// Global prefetch: hover with 100ms delay
|
|
@@ -145,7 +145,7 @@ Function provided to `beforeLoad` for programmatic redirection.
|
|
|
145
145
|
|
|
146
146
|
**Type:** `(arg: Location | string) => Promise<void>`
|
|
147
147
|
|
|
148
|
-
```
|
|
148
|
+
```tsx
|
|
149
149
|
import type { createRouter } from 'clear-react-router';
|
|
150
150
|
|
|
151
151
|
const routes = createRouter([
|
|
@@ -189,7 +189,9 @@ const routes = createRouter([
|
|
|
189
189
|
|
|
190
190
|
The `loader`, `beforeLoad`, and `afterLoad` hooks receive `params` (extracted from the URL) and `context` as arguments. This allows you to handle route-specific logic directly in the route configuration, keeping your components focused on rendering.
|
|
191
191
|
|
|
192
|
-
```
|
|
192
|
+
```tsx
|
|
193
|
+
import type { createRouter } from 'clear-react-router';
|
|
194
|
+
|
|
193
195
|
const routes = createRouter([
|
|
194
196
|
{
|
|
195
197
|
path: '/user/:userId',
|
|
@@ -240,7 +242,7 @@ const App = () => (
|
|
|
240
242
|
|
|
241
243
|
Returns function to navigate programmatically. Accepts a string (pathname), an object with `pathname`, `search`, and `state`, or `-1` to go back.
|
|
242
244
|
|
|
243
|
-
```
|
|
245
|
+
```tsx
|
|
244
246
|
const navigate = useNavigate();
|
|
245
247
|
|
|
246
248
|
navigate('/about'); // string
|
|
@@ -250,7 +252,7 @@ navigate(-1); // go back
|
|
|
250
252
|
|
|
251
253
|
**Note:** Navigation state can be accessed via `useLocation()`:
|
|
252
254
|
|
|
253
|
-
```
|
|
255
|
+
```tsx
|
|
254
256
|
const navigate = useNavigate();
|
|
255
257
|
navigate({ pathname: '/profile', state: { userId: 123 } });
|
|
256
258
|
|
|
@@ -263,7 +265,7 @@ console.log(state); // { userId: 123 }
|
|
|
263
265
|
|
|
264
266
|
Returns route parameters object.
|
|
265
267
|
|
|
266
|
-
```
|
|
268
|
+
```tsx
|
|
267
269
|
const params = useParams<{ userId: string }>();
|
|
268
270
|
// URL: /user/123 → params.userId === '123'
|
|
269
271
|
```
|
|
@@ -271,7 +273,7 @@ const params = useParams<{ userId: string }>();
|
|
|
271
273
|
### `useLocation()`
|
|
272
274
|
|
|
273
275
|
Returns current location `{ pathname, search, state }`.
|
|
274
|
-
```
|
|
276
|
+
```tsx
|
|
275
277
|
const { pathname, search, state } = useLocation();
|
|
276
278
|
```
|
|
277
279
|
|
|
@@ -287,7 +289,7 @@ Returns the cached data loaded by the current route's `loader`, along with any e
|
|
|
287
289
|
| `loaderError` | `Error \| null` | Error from the `loader` (if any) |
|
|
288
290
|
| `beforeLoadError` | `Error \| null` | Error from the `beforeLoad` hook (if any) |
|
|
289
291
|
|
|
290
|
-
```
|
|
292
|
+
```tsx
|
|
291
293
|
const UserProfile = () => {
|
|
292
294
|
const { data, loaderError, beforeLoadError } = useLoaderState<User>();
|
|
293
295
|
```
|
|
@@ -380,7 +382,7 @@ Blocks navigation when callback returns `true`.
|
|
|
380
382
|
| `process()` | `() => void` | Confirm navigation and proceed |
|
|
381
383
|
| `reset()` | `() => void` | Cancel navigation |
|
|
382
384
|
|
|
383
|
-
```
|
|
385
|
+
```tsx
|
|
384
386
|
const { state, process, reset } = useBlocker(() => hasUnsavedChanges);
|
|
385
387
|
|
|
386
388
|
useEffect(() => {
|
|
@@ -407,7 +409,7 @@ Executes a callback when the page is about to be closed or reloaded. Perfect for
|
|
|
407
409
|
|
|
408
410
|
**Note:** This hook does not show a browser confirmation dialog. It silently executes the callback, allowing you to save user data in the background before the page closes.
|
|
409
411
|
|
|
410
|
-
```
|
|
412
|
+
```tsx
|
|
411
413
|
const [text, setText] = useState('');
|
|
412
414
|
const onSave = useCallback(() => {
|
|
413
415
|
localStorage.setItem('draft', text);
|
|
@@ -422,7 +424,7 @@ useBeforeUnload(text ? onSave : undefined);
|
|
|
422
424
|
|
|
423
425
|
A flexible hook for working with typed query parameters. You provide an adapter object with `parse` and `serialize` functions, and it returns the parsed value and a setter.
|
|
424
426
|
|
|
425
|
-
```
|
|
427
|
+
```tsx
|
|
426
428
|
import { useQueryParam, adapter } from 'clear-react-router';
|
|
427
429
|
|
|
428
430
|
const ProductPage = () => {
|
|
@@ -486,7 +488,7 @@ type Adapter<T> = {
|
|
|
486
488
|
### Using Zod Schemas
|
|
487
489
|
`useQueryParam` works seamlessly with Zod for complex validation:
|
|
488
490
|
|
|
489
|
-
```
|
|
491
|
+
```tsx
|
|
490
492
|
import { z } from 'zod';
|
|
491
493
|
import { useQueryParam, adapter } from 'clear-react-router';
|
|
492
494
|
|
|
@@ -518,7 +520,7 @@ function ProductFilter() {
|
|
|
518
520
|
### Custom Adapters
|
|
519
521
|
You can write your own adapter for any format:
|
|
520
522
|
|
|
521
|
-
```
|
|
523
|
+
```tsx
|
|
522
524
|
// Custom adapter for comma-separated values
|
|
523
525
|
const csvAdapter = {
|
|
524
526
|
parse: (params: string[]): string[] => {
|
|
@@ -537,7 +539,7 @@ const TagsFilter() {
|
|
|
537
539
|
### `useRouterContext()`
|
|
538
540
|
|
|
539
541
|
Returns the router context object and a function to update it. Useful for accessing or modifying global state (like user authentication, theme, etc.) from anywhere in your app.
|
|
540
|
-
```
|
|
542
|
+
```tsx
|
|
541
543
|
const { setContext, context } = useRouterContext();
|
|
542
544
|
const loginHandler = () => setContext({ ...context, user: { name: 'John' } });
|
|
543
545
|
```
|
|
@@ -546,7 +548,7 @@ const loginHandler = () => setContext({ ...context, user: { name: 'John' } });
|
|
|
546
548
|
|
|
547
549
|
Returns an object for working with URL query parameters. Supports reading and setting both single values and arrays.
|
|
548
550
|
|
|
549
|
-
```
|
|
551
|
+
```tsx
|
|
550
552
|
import { useSearchParams } from 'clear-react-router';
|
|
551
553
|
|
|
552
554
|
function ProductFilter() {
|
|
@@ -603,7 +605,7 @@ Returns an array of pathnames representing the user's actual navigation history.
|
|
|
603
605
|
## Lazy Loading
|
|
604
606
|
|
|
605
607
|
Clear Router supports code-splitting out of the box. Simply pass a function that returns a dynamic import:
|
|
606
|
-
```
|
|
608
|
+
```tsx
|
|
607
609
|
{
|
|
608
610
|
path: '/heavy-page',
|
|
609
611
|
element: () => import('./pages/HeavyComponent'),
|