@sasha.p/react-router-persistent-state 0.0.1 → 0.1.0
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/CHANGELOG.md +38 -0
- package/README.md +135 -0
- package/dist/index.d.ts +16 -3
- package/dist/react-router-persistent-state.es.js +35 -1759
- package/dist/react-router-persistent-state.umd.js +1 -19
- package/package.json +36 -35
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
All notable changes to this project are documented in this file.
|
|
4
|
+
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
|
5
|
+
|
|
6
|
+
## [0.1.0] - 2026-12-06
|
|
7
|
+
|
|
8
|
+
Requires `@sasha.p/use-persistent-state` 1.1.0 or newer.
|
|
9
|
+
|
|
10
|
+
### Added
|
|
11
|
+
|
|
12
|
+
- `emptyValues: 'legacy' | 'preserve'` option for `useSearchParamsState` (next to React Router's `NavigateOptions`) and `getPersistedValues`. With `'preserve'`, `''` is kept in the URL and read back as `''`. Without this option, the core's one-time deprecation warning for the `'legacy'` mode can't be avoided.
|
|
13
|
+
- `SearchParamsStateOptions` type (`NavigateOptions & EmptyValuesOption`).
|
|
14
|
+
- A README.
|
|
15
|
+
|
|
16
|
+
### Changed
|
|
17
|
+
|
|
18
|
+
- The peer dependency on `@sasha.p/use-persistent-state` is now `^1.1.0` (was `>=1.0.5`).
|
|
19
|
+
- React Router and `@sasha.p/use-persistent-state` are no longer bundled: the package uses the app's copies. The ES bundle shrank from 56 kB to about 1 kB.
|
|
20
|
+
- No runtime dependencies: `lodash.pickby` was removed.
|
|
21
|
+
- The package is marked `sideEffects: false`.
|
|
22
|
+
|
|
23
|
+
### Fixed
|
|
24
|
+
|
|
25
|
+
- Importing the package with React Router 7 failed: `The requested module 'react-router' does not provide an export named 'UNSAFE_useRouteId'` ([#1](https://github.com/stushurik/react-router-persistent-state/pull/1)).
|
|
26
|
+
- TypeScript couldn't find the types with `moduleResolution: "bundler"` (or `node16`) ([#1](https://github.com/stushurik/react-router-persistent-state/pull/1)).
|
|
27
|
+
- Fixes in `@sasha.p/use-persistent-state` didn't reach users, because a copy of version 1.0.5 was bundled ([#1](https://github.com/stushurik/react-router-persistent-state/pull/1)).
|
|
28
|
+
- `setState` dropped query params that were changed elsewhere after mount ([#2](https://github.com/stushurik/react-router-persistent-state/pull/2)).
|
|
29
|
+
- `getPersistedValues` threw during server-side rendering. It now returns the defaults ([#2](https://github.com/stushurik/react-router-persistent-state/pull/2)).
|
|
30
|
+
- `state` in `NavigateOptions` lost values that don't survive a JSON round trip, such as `Date` ([#2](https://github.com/stushurik/react-router-persistent-state/pull/2)).
|
|
31
|
+
- Crash in browsers without Iterator helpers (before Chrome 122 and Safari 18.4) ([#2](https://github.com/stushurik/react-router-persistent-state/pull/2)).
|
|
32
|
+
|
|
33
|
+
## [0.0.1] - 2025-09-23
|
|
34
|
+
|
|
35
|
+
Initial release.
|
|
36
|
+
|
|
37
|
+
[0.1.0]: https://github.com/stushurik/react-router-persistent-state/compare/ac156f9...v0.1.0
|
|
38
|
+
[0.0.1]: https://github.com/stushurik/react-router-persistent-state/tree/ac156f9
|
package/README.md
ADDED
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
# React Router Persistent State
|
|
2
|
+
|
|
3
|
+
A `useState`-like hook that keeps state in the URL query string through **React Router**. It's the React Router flavour of `useSearchParamsState` from [`@sasha.p/use-persistent-state`](https://github.com/stushurik/use-persistent-state), and it works with the same schemas and data types.
|
|
4
|
+
|
|
5
|
+
- 🔒 **Type-safe**: the state type is inferred from the schema
|
|
6
|
+
- 🧭 **Goes through the router**: updates use React Router's navigation (`replace`, `state`, ...) instead of writing to `window.history`
|
|
7
|
+
- 🧩 **One query param per field**, with defaults and `null` for missing values
|
|
8
|
+
- 📦 Tiny: about 1 kB, no runtime dependencies
|
|
9
|
+
|
|
10
|
+
---
|
|
11
|
+
|
|
12
|
+
## Installation
|
|
13
|
+
|
|
14
|
+
```bash
|
|
15
|
+
npm i @sasha.p/react-router-persistent-state @sasha.p/use-persistent-state
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
Peer dependencies:
|
|
19
|
+
|
|
20
|
+
| Package | Version |
|
|
21
|
+
| ------------------------------- | -------------------- |
|
|
22
|
+
| `@sasha.p/use-persistent-state` | `^1.1.0` |
|
|
23
|
+
| `react-router-dom` | `^6.0.0 \|\| ^7.0.0` |
|
|
24
|
+
| `react`, `react-dom` | `>=18.2.0` |
|
|
25
|
+
|
|
26
|
+
The hook must be rendered inside a React Router router (`<BrowserRouter>`, `createBrowserRouter`, ...).
|
|
27
|
+
|
|
28
|
+
---
|
|
29
|
+
|
|
30
|
+
## Quick start
|
|
31
|
+
|
|
32
|
+
```tsx
|
|
33
|
+
import { $Number, $String } from '@sasha.p/use-persistent-state';
|
|
34
|
+
import { useSearchParamsState } from '@sasha.p/react-router-persistent-state';
|
|
35
|
+
|
|
36
|
+
const schema = {
|
|
37
|
+
page: $Number({ defaultValue: 1 }),
|
|
38
|
+
query: $String(),
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
function ProductList() {
|
|
42
|
+
// state: { page: number; query: string | null }
|
|
43
|
+
const [state, setState] = useSearchParamsState(schema);
|
|
44
|
+
|
|
45
|
+
return (
|
|
46
|
+
<>
|
|
47
|
+
<input
|
|
48
|
+
value={state.query ?? ''}
|
|
49
|
+
onChange={(e) =>
|
|
50
|
+
setState((prev) => ({ ...prev, query: e.target.value, page: 1 }))
|
|
51
|
+
}
|
|
52
|
+
/>
|
|
53
|
+
<button
|
|
54
|
+
onClick={() => setState((prev) => ({ ...prev, page: prev.page + 1 }))}
|
|
55
|
+
>
|
|
56
|
+
Next page
|
|
57
|
+
</button>
|
|
58
|
+
</>
|
|
59
|
+
);
|
|
60
|
+
}
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
`/products?page=3&query=shoes` gives `{ page: 3, query: 'shoes' }`; `/products` gives `{ page: 1, query: null }`.
|
|
64
|
+
|
|
65
|
+
Data types (`$String`, `$Number`, `$Boolean`, `$StringArray`, `$NumberArray`, custom ones from `createDataTypeCtr`) come from `@sasha.p/use-persistent-state`. See [its README](https://github.com/stushurik/use-persistent-state#readme) for the full list and their options.
|
|
66
|
+
|
|
67
|
+
---
|
|
68
|
+
|
|
69
|
+
## `useSearchParamsState(schema, options?)`
|
|
70
|
+
|
|
71
|
+
Returns `[state, setState]`, like `useState`.
|
|
72
|
+
|
|
73
|
+
### Updating state
|
|
74
|
+
|
|
75
|
+
`setState` accepts an object or a function of the previous state. It **replaces** the state: fields left out of the update are reset to their defaults (or `null`).
|
|
76
|
+
|
|
77
|
+
```ts
|
|
78
|
+
// state: { page: 3, query: 'shoes' }
|
|
79
|
+
setState({ page: 4 });
|
|
80
|
+
// state: { page: 4, query: null } ← query was reset
|
|
81
|
+
|
|
82
|
+
setState((prev) => ({ ...prev, page: 4 }));
|
|
83
|
+
// state: { page: 4, query: 'shoes' } ← query was kept
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
Each update navigates to the same path with new query params:
|
|
87
|
+
|
|
88
|
+
- Query params that aren't in the schema are kept.
|
|
89
|
+
- Fields that are `null` are removed from the URL.
|
|
90
|
+
- Empty values (`''`) are removed too, unless you use `emptyValues: 'preserve'`.
|
|
91
|
+
|
|
92
|
+
### Options
|
|
93
|
+
|
|
94
|
+
`options` are React Router's `NavigateOptions` (the options of `navigate()`) plus `emptyValues`:
|
|
95
|
+
|
|
96
|
+
```ts
|
|
97
|
+
const [state, setState] = useSearchParamsState(schema, {
|
|
98
|
+
replace: true, // replace the history entry instead of pushing a new one
|
|
99
|
+
state: { from: 'filters' }, // location.state for the navigation
|
|
100
|
+
emptyValues: 'preserve',
|
|
101
|
+
});
|
|
102
|
+
```
|
|
103
|
+
|
|
104
|
+
`emptyValues: 'legacy' | 'preserve'` controls whether empty values are kept: with `'preserve'`, `?query=` reads as `''` and `''` is written to the URL, while a missing param gives the default (or `null`). The default, `'legacy'`, treats `''` as missing; it's deprecated and logs a one-time warning in development. See [Empty values](https://github.com/stushurik/use-persistent-state#empty-values) in the core README.
|
|
105
|
+
|
|
106
|
+
---
|
|
107
|
+
|
|
108
|
+
## `getPersistedValues(schema, options?)`
|
|
109
|
+
|
|
110
|
+
Reads the schema's values from the current URL outside React, e.g. in an event handler or a test. It reads `document.location`, so it sees the browser's URL, not the location of a `MemoryRouter`. On the server it returns the defaults.
|
|
111
|
+
|
|
112
|
+
```ts
|
|
113
|
+
import { getPersistedValues } from '@sasha.p/react-router-persistent-state';
|
|
114
|
+
|
|
115
|
+
const { page, query } = getPersistedValues(schema);
|
|
116
|
+
const values = getPersistedValues(schema, { emptyValues: 'preserve' });
|
|
117
|
+
```
|
|
118
|
+
|
|
119
|
+
---
|
|
120
|
+
|
|
121
|
+
## This package or the core's `useSearchParamsState`?
|
|
122
|
+
|
|
123
|
+
| | `@sasha.p/react-router-persistent-state` | `useSearchParamsState` from `@sasha.p/use-persistent-state` |
|
|
124
|
+
| ------------------ | ---------------------------------------- | ----------------------------------------------------------- |
|
|
125
|
+
| Router | React Router 6 / 7 | none, writes to `window.history` |
|
|
126
|
+
| Navigation options | React Router's `NavigateOptions` | `{ replace }` |
|
|
127
|
+
| Location source | the router (works with `MemoryRouter`) | `window.location` |
|
|
128
|
+
|
|
129
|
+
Use this package in apps built on React Router, so URL changes go through the router and its loaders, blockers and `useLocation` stay in sync.
|
|
130
|
+
|
|
131
|
+
---
|
|
132
|
+
|
|
133
|
+
## Limitations
|
|
134
|
+
|
|
135
|
+
- **The state is read from the URL on mount only.** Navigating elsewhere (browser back/forward, `<Link>`, `navigate()`) changes the URL, but not the hook's state. This comes from the core and is planned to be fixed in its next major version ([use-persistent-state#6](https://github.com/stushurik/use-persistent-state/issues/6)).
|
package/dist/index.d.ts
CHANGED
|
@@ -1,4 +1,17 @@
|
|
|
1
|
-
import { SchemaFromData, usePersistentState } from '@sasha.p/use-persistent-state';
|
|
1
|
+
import { EmptyValuesOption, SchemaFromData, usePersistentState } from '@sasha.p/use-persistent-state';
|
|
2
2
|
import { NavigateOptions } from 'react-router-dom';
|
|
3
|
-
export
|
|
4
|
-
|
|
3
|
+
export type SearchParamsStateOptions = NavigateOptions & EmptyValuesOption;
|
|
4
|
+
/**
|
|
5
|
+
* `useSearchParamsState` from @sasha.p/use-persistent-state, built on React
|
|
6
|
+
* Router's `useSearchParams`: every schema field is one query param, and
|
|
7
|
+
* updates go through the router's navigation.
|
|
8
|
+
*
|
|
9
|
+
* `options` are React Router's `NavigateOptions` (`replace`, `state`, ...)
|
|
10
|
+
* plus `emptyValues` (see @sasha.p/use-persistent-state).
|
|
11
|
+
*/
|
|
12
|
+
export declare function useSearchParamsState<T extends object>(schema: SchemaFromData<T>, options?: SearchParamsStateOptions): ReturnType<typeof usePersistentState<T>>;
|
|
13
|
+
/**
|
|
14
|
+
* Reads the schema's values from the current URL, outside React. Returns the
|
|
15
|
+
* defaults on the server.
|
|
16
|
+
*/
|
|
17
|
+
export declare function getPersistedValues<T extends object>(schema: SchemaFromData<T>, options?: EmptyValuesOption): T;
|