state-in-url 6.1.3 → 6.2.1
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 +11 -0
- package/dist/package.json +9 -6
- package/package.json +9 -6
- package/skills/feature-state-hook/SKILL.md +352 -0
- package/skills/form-library-integration/SKILL.md +191 -0
- package/skills/input-handling/SKILL.md +160 -0
- package/skills/nextjs-ssr/SKILL.md +251 -0
- package/skills/react-router-remix-setup/SKILL.md +180 -0
- package/skills/shared-state-no-url/SKILL.md +158 -0
|
@@ -0,0 +1,160 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: input-handling
|
|
3
|
+
description: >
|
|
4
|
+
Reconcile instant local input feedback with URL write throttling. setState
|
|
5
|
+
updates internal state synchronously (instant re-render); setUrl is throttled
|
|
6
|
+
and asynchronous (URL catches up on the next tick). Use this skill for text
|
|
7
|
+
inputs, search boxes, sliders, range pickers, and any control that fires
|
|
8
|
+
many updates per second where binding setUrl directly to onChange causes
|
|
9
|
+
perceived lag or wasted URL writes.
|
|
10
|
+
type: core
|
|
11
|
+
library: state-in-url
|
|
12
|
+
library_version: '6.1.3'
|
|
13
|
+
requires:
|
|
14
|
+
- feature-state-hook
|
|
15
|
+
sources:
|
|
16
|
+
- 'asmyshlyaev177/state-in-url:packages/urlstate/useUrlStateBase/useUrlStateBase.ts'
|
|
17
|
+
- 'asmyshlyaev177/state-in-url:packages/urlstate/utils.ts'
|
|
18
|
+
- 'asmyshlyaev177/state-in-url:README.md#update-state-only-and-sync-to-url-manually'
|
|
19
|
+
---
|
|
20
|
+
|
|
21
|
+
This skill builds on `state-in-url/feature-state-hook`. Read it first for the module-scoped default-state rule.
|
|
22
|
+
|
|
23
|
+
# state-in-url — Input handling
|
|
24
|
+
|
|
25
|
+
`useUrlState` returns three setters with different timing:
|
|
26
|
+
|
|
27
|
+
| Setter | What updates | When |
|
|
28
|
+
|---|---|---|
|
|
29
|
+
| `setState(value)` | Internal state only | Synchronous |
|
|
30
|
+
| `setUrl(value)` | Internal state + URL | State sync, URL on next tick (throttled) |
|
|
31
|
+
| `setUrl()` | Flush current state to URL | URL on next tick (diff-checked, no-op if equal) |
|
|
32
|
+
|
|
33
|
+
State updates always render immediately. URL writes are coalesced through an internal global timer (`TIMEOUT` constant in [useUrlStateBase.ts](https://github.com/asmyshlyaev177/state-in-url/blob/master/packages/urlstate/useUrlStateBase/useUrlStateBase.ts)) so a burst of `setUrl` calls produces one URL update.
|
|
34
|
+
|
|
35
|
+
## Setup
|
|
36
|
+
|
|
37
|
+
```typescript
|
|
38
|
+
// features/search/searchState.ts
|
|
39
|
+
export type SearchState = { q: string; sort: 'name' | 'date' };
|
|
40
|
+
export const SEARCH_STATE: SearchState = { q: '', sort: 'name' };
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
```typescript
|
|
44
|
+
// features/search/useSearchState.ts
|
|
45
|
+
'use client';
|
|
46
|
+
import { useSearchParams } from 'next/navigation';
|
|
47
|
+
import { useUrlState } from 'state-in-url/next';
|
|
48
|
+
import { SEARCH_STATE } from './searchState';
|
|
49
|
+
|
|
50
|
+
export function useSearchState() {
|
|
51
|
+
const searchParams = useSearchParams();
|
|
52
|
+
return useUrlState(SEARCH_STATE, { searchParams });
|
|
53
|
+
}
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
## Core Patterns
|
|
57
|
+
|
|
58
|
+
### Instant input, deferred URL write (onBlur)
|
|
59
|
+
|
|
60
|
+
```typescript
|
|
61
|
+
function SearchBox() {
|
|
62
|
+
const { urlState, setState, setUrl } = useSearchState();
|
|
63
|
+
|
|
64
|
+
return (
|
|
65
|
+
<input
|
|
66
|
+
value={urlState.q}
|
|
67
|
+
onChange={(e) => setState({ q: e.target.value })}
|
|
68
|
+
onBlur={() => setUrl()}
|
|
69
|
+
/>
|
|
70
|
+
);
|
|
71
|
+
}
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
`setState` updates state instantly (input feels native). `setUrl()` with no args flushes current state to the URL when the user finishes typing.
|
|
75
|
+
|
|
76
|
+
### Discrete controls — `setUrl` directly
|
|
77
|
+
|
|
78
|
+
For click/select/toggle controls that fire at most a few times per second, skip the split. `setUrl` is already throttled.
|
|
79
|
+
|
|
80
|
+
```typescript
|
|
81
|
+
<button onClick={() => setUrl({ sort: 'date' })}>Sort by date</button>
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
## Common Mistakes
|
|
85
|
+
|
|
86
|
+
### CRITICAL `setUrl` inside `useEffect` → infinite update loop
|
|
87
|
+
|
|
88
|
+
(Cross-skill failure — also in `feature-state-hook`.)
|
|
89
|
+
|
|
90
|
+
Wrong:
|
|
91
|
+
|
|
92
|
+
```typescript
|
|
93
|
+
React.useEffect(() => {
|
|
94
|
+
setUrl({ q: urlState.q.trim() }); // re-fires every URL change
|
|
95
|
+
}, [urlState, setUrl]);
|
|
96
|
+
```
|
|
97
|
+
|
|
98
|
+
Correct:
|
|
99
|
+
|
|
100
|
+
```typescript
|
|
101
|
+
React.useEffect(() => {
|
|
102
|
+
const trimmed = urlState.q.trim();
|
|
103
|
+
if (urlState.q !== trimmed) setUrl({ q: trimmed });
|
|
104
|
+
}, [urlState.q, setUrl]);
|
|
105
|
+
```
|
|
106
|
+
|
|
107
|
+
URL throttling does not break a state→effect→setUrl→state cycle.
|
|
108
|
+
|
|
109
|
+
Source: Maintainer interview
|
|
110
|
+
|
|
111
|
+
### HIGH Binding `setUrl` directly to onChange of a typing input
|
|
112
|
+
|
|
113
|
+
Wrong:
|
|
114
|
+
|
|
115
|
+
```typescript
|
|
116
|
+
<input
|
|
117
|
+
value={urlState.q}
|
|
118
|
+
onChange={(e) => setUrl({ q: e.target.value })}
|
|
119
|
+
/>
|
|
120
|
+
```
|
|
121
|
+
|
|
122
|
+
Correct:
|
|
123
|
+
|
|
124
|
+
```typescript
|
|
125
|
+
<input
|
|
126
|
+
value={urlState.q}
|
|
127
|
+
onChange={(e) => setState({ q: e.target.value })}
|
|
128
|
+
onBlur={() => setUrl()}
|
|
129
|
+
/>
|
|
130
|
+
```
|
|
131
|
+
|
|
132
|
+
Every keystroke fires a URL update. The library coalesces them with an internal timer, but the perceived behavior is laggy URL with extra rerenders (one for state, another when URL settles). Issue #78 was filed exactly for this symptom — the throttling is intentional.
|
|
133
|
+
|
|
134
|
+
Source: GitHub issue #78 (asmyshlyaev177/state-in-url); README "Update state only and sync to URL manually"
|
|
135
|
+
|
|
136
|
+
### MEDIUM Expecting `window.location.search` to reflect `setUrl` synchronously
|
|
137
|
+
|
|
138
|
+
Wrong:
|
|
139
|
+
|
|
140
|
+
```typescript
|
|
141
|
+
setUrl({ tab: 'b' });
|
|
142
|
+
console.log(window.location.search); // still the old value
|
|
143
|
+
```
|
|
144
|
+
|
|
145
|
+
Correct:
|
|
146
|
+
|
|
147
|
+
```typescript
|
|
148
|
+
setUrl({ tab: 'b' });
|
|
149
|
+
// `urlState.tab` already equals 'b' synchronously.
|
|
150
|
+
// `window.location.search` catches up after the throttle tick.
|
|
151
|
+
```
|
|
152
|
+
|
|
153
|
+
`setUrl` is "last-write-wins" — it coalesces a burst of updates into one URL write on the next macrotask. Read `urlState`, not `window.location`.
|
|
154
|
+
|
|
155
|
+
Source: useUrlStateBase.ts (global `timer`); GitHub issue #78
|
|
156
|
+
|
|
157
|
+
## See also
|
|
158
|
+
|
|
159
|
+
- `state-in-url/feature-state-hook` — base pattern this skill builds on.
|
|
160
|
+
- `state-in-url/nextjs-ssr` — for SSR-safe wiring of the search input on Next.js App Router.
|
|
@@ -0,0 +1,251 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: nextjs-ssr
|
|
3
|
+
description: >
|
|
4
|
+
SSR-safe useUrlState in Next.js App Router. Forward searchParams from server
|
|
5
|
+
pages (awaiting the Promise in Next.js 15+), call useSearchParams() in pure
|
|
6
|
+
client components, decide between useHistory true/false, and use a Proxy
|
|
7
|
+
(formerly middleware) to expose query params to server layouts. App Router
|
|
8
|
+
only — Pages Router is not supported. Load this skill for any use of
|
|
9
|
+
state-in-url/next or anytime URL state must be correct on first paint.
|
|
10
|
+
type: framework
|
|
11
|
+
library: state-in-url
|
|
12
|
+
framework: react
|
|
13
|
+
library_version: '6.1.3'
|
|
14
|
+
requires:
|
|
15
|
+
- feature-state-hook
|
|
16
|
+
sources:
|
|
17
|
+
- 'asmyshlyaev177/state-in-url:packages/urlstate/next/useUrlState/useUrlState.ts'
|
|
18
|
+
- 'asmyshlyaev177/state-in-url:packages/example-nextjs16/src/middleware.ts'
|
|
19
|
+
- 'asmyshlyaev177/state-in-url:README.md#with-server-side-rendering'
|
|
20
|
+
---
|
|
21
|
+
|
|
22
|
+
This skill builds on `state-in-url/feature-state-hook`. Read it first for the module-scoped default-state rule.
|
|
23
|
+
|
|
24
|
+
# state-in-url — Next.js App Router SSR
|
|
25
|
+
|
|
26
|
+
Without `searchParams`, the first render of a `useUrlState` component has no URL knowledge. It renders defaults, then a client `useEffect` re-syncs from the URL → visible flash and a React hydration warning. The fix is to feed the URL into the hook on the server (via `searchParams` prop or a Proxy header) so the very first render is correct.
|
|
27
|
+
|
|
28
|
+
App Router only. Pages Router uses `next/router`, which `state-in-url/next` does not support — there are no plans to add it.
|
|
29
|
+
|
|
30
|
+
## Setup
|
|
31
|
+
|
|
32
|
+
### Server page forwarding `searchParams` (recommended)
|
|
33
|
+
|
|
34
|
+
```typescript
|
|
35
|
+
// app/jobs/page.tsx (Server Component)
|
|
36
|
+
import { JobsList } from './JobsList';
|
|
37
|
+
|
|
38
|
+
export default async function Page({
|
|
39
|
+
searchParams,
|
|
40
|
+
}: {
|
|
41
|
+
searchParams: Promise<Record<string, string | string[] | undefined>>;
|
|
42
|
+
}) {
|
|
43
|
+
const sp = await searchParams; // Next.js 15+: searchParams is a Promise
|
|
44
|
+
return <JobsList searchParams={sp} />;
|
|
45
|
+
}
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
```typescript
|
|
49
|
+
// app/jobs/JobsList.tsx
|
|
50
|
+
'use client';
|
|
51
|
+
import { useUrlState } from 'state-in-url/next';
|
|
52
|
+
import { JOBS_STATE } from 'features/jobs/jobsState';
|
|
53
|
+
|
|
54
|
+
export function JobsList({ searchParams }: { searchParams: object }) {
|
|
55
|
+
const { urlState, setUrl } = useUrlState(JOBS_STATE, { searchParams });
|
|
56
|
+
return <pre>{JSON.stringify(urlState)}</pre>;
|
|
57
|
+
}
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
### Pure client component using `useSearchParams`
|
|
61
|
+
|
|
62
|
+
When you can't (or don't want to) thread `searchParams` from a server parent:
|
|
63
|
+
|
|
64
|
+
```typescript
|
|
65
|
+
'use client';
|
|
66
|
+
import { useSearchParams } from 'next/navigation';
|
|
67
|
+
import { useUrlState } from 'state-in-url/next';
|
|
68
|
+
import { JOBS_STATE } from 'features/jobs/jobsState';
|
|
69
|
+
|
|
70
|
+
export function useJobsState() {
|
|
71
|
+
const searchParams = useSearchParams();
|
|
72
|
+
return useUrlState(JOBS_STATE, { searchParams });
|
|
73
|
+
}
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
## Core Patterns
|
|
77
|
+
|
|
78
|
+
### `useHistory` — choosing the URL update mode
|
|
79
|
+
|
|
80
|
+
| Setting | Mechanism | Effect | Use when |
|
|
81
|
+
|---|---|---|---|
|
|
82
|
+
| `useHistory: true` (default) | `window.history.pushState` / `replaceState` | No `_rsc` round-trip per URL change | URL state is client-only UI (filters, drawers, tabs) |
|
|
83
|
+
| `useHistory: false` | `next/navigation`'s `router.push` / `router.replace` | RSC refetches on every URL change | URL state must refetch server data (e.g. `searchParams` drives a DB query in the page) |
|
|
84
|
+
|
|
85
|
+
```typescript
|
|
86
|
+
useUrlState(FORM_STATE, { searchParams, useHistory: false });
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
Default is `true`. Flip to `false` only when the server page needs to re-render with the new query.
|
|
90
|
+
|
|
91
|
+
### Decoding state on the server for data fetching
|
|
92
|
+
|
|
93
|
+
```typescript
|
|
94
|
+
// app/jobs/page.tsx
|
|
95
|
+
import { decodeState } from 'state-in-url/encodeState';
|
|
96
|
+
import { JOBS_STATE } from 'features/jobs/jobsState';
|
|
97
|
+
import { fetchJobs } from 'lib/jobs';
|
|
98
|
+
|
|
99
|
+
export default async function Page({ searchParams }) {
|
|
100
|
+
const sp = await searchParams;
|
|
101
|
+
const state = decodeState(new URLSearchParams(sp as Record<string, string>), JOBS_STATE);
|
|
102
|
+
const jobs = await fetchJobs({ status: state.status });
|
|
103
|
+
return <JobsList searchParams={sp} jobs={jobs} />;
|
|
104
|
+
}
|
|
105
|
+
```
|
|
106
|
+
|
|
107
|
+
### Reading URL state in a server layout (Proxy workaround)
|
|
108
|
+
|
|
109
|
+
Server layouts don't receive `searchParams`. Set up a Proxy (Next.js 16+ — `middleware.ts` still works as a deprecated alias) to surface the query string as a request header, then decode in the layout. With this setup the layout renders correctly on first paint with no extra rerenders.
|
|
110
|
+
|
|
111
|
+
```typescript
|
|
112
|
+
// proxy.ts (or middleware.ts, same content)
|
|
113
|
+
import type { NextRequest } from 'next/server';
|
|
114
|
+
import { NextResponse } from 'next/server';
|
|
115
|
+
|
|
116
|
+
export function proxy(request: NextRequest) {
|
|
117
|
+
const sp = (request.url.includes('_next') ? '' : request.url).split('?')[1] ?? '';
|
|
118
|
+
const headers = new Headers(request.headers);
|
|
119
|
+
headers.set('searchParams', sp);
|
|
120
|
+
return NextResponse.next({ request: { headers } });
|
|
121
|
+
}
|
|
122
|
+
```
|
|
123
|
+
|
|
124
|
+
```typescript
|
|
125
|
+
// app/jobs/layout.tsx
|
|
126
|
+
import { headers } from 'next/headers';
|
|
127
|
+
import { decodeState } from 'state-in-url/encodeState';
|
|
128
|
+
import { JOBS_STATE } from 'features/jobs/jobsState';
|
|
129
|
+
|
|
130
|
+
export default async function Layout({ children }: { children: React.ReactNode }) {
|
|
131
|
+
const sp = (await headers()).get('searchParams') ?? '';
|
|
132
|
+
const initial = decodeState(sp, JOBS_STATE);
|
|
133
|
+
|
|
134
|
+
return (
|
|
135
|
+
<>
|
|
136
|
+
<LayoutHeader initial={initial} />
|
|
137
|
+
{children}
|
|
138
|
+
</>
|
|
139
|
+
);
|
|
140
|
+
}
|
|
141
|
+
```
|
|
142
|
+
|
|
143
|
+
## Common Mistakes
|
|
144
|
+
|
|
145
|
+
### CRITICAL Not passing `searchParams` in Next.js App Router
|
|
146
|
+
|
|
147
|
+
Wrong:
|
|
148
|
+
|
|
149
|
+
```typescript
|
|
150
|
+
// page.tsx (server)
|
|
151
|
+
export default function Page() { return <Form />; }
|
|
152
|
+
|
|
153
|
+
// Form.tsx (client)
|
|
154
|
+
const { urlState } = useUrlState(FORM_STATE); // missing searchParams
|
|
155
|
+
```
|
|
156
|
+
|
|
157
|
+
Correct:
|
|
158
|
+
|
|
159
|
+
```typescript
|
|
160
|
+
// page.tsx
|
|
161
|
+
export default async function Page({ searchParams }) {
|
|
162
|
+
const sp = await searchParams;
|
|
163
|
+
return <Form searchParams={sp} />;
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
// Form.tsx
|
|
167
|
+
const { urlState } = useUrlState(FORM_STATE, { searchParams });
|
|
168
|
+
```
|
|
169
|
+
|
|
170
|
+
`searchParams` makes the **initial** state correct. Without it, the first render uses defaults, then a client `useEffect` re-syncs from the URL on the next tick — causing a visible flash and a hydration warning. URL state still survives refresh either way (URL is the source of truth), but the initial paint is wrong. With a Proxy feeding the layout, no extra rerender happens at all. Most common Next.js issue (#40, #60).
|
|
171
|
+
|
|
172
|
+
Source: GitHub issues #40, #60 (asmyshlyaev177/state-in-url); maintainer interview
|
|
173
|
+
|
|
174
|
+
### HIGH Forgetting to await `searchParams` in Next.js 15+
|
|
175
|
+
|
|
176
|
+
Wrong:
|
|
177
|
+
|
|
178
|
+
```typescript
|
|
179
|
+
export default function Page({ searchParams }) {
|
|
180
|
+
return <Form searchParams={searchParams} />; // Promise, not the value
|
|
181
|
+
}
|
|
182
|
+
```
|
|
183
|
+
|
|
184
|
+
Correct:
|
|
185
|
+
|
|
186
|
+
```typescript
|
|
187
|
+
export default async function Page({ searchParams }) {
|
|
188
|
+
const sp = await searchParams;
|
|
189
|
+
return <Form searchParams={sp} />;
|
|
190
|
+
}
|
|
191
|
+
```
|
|
192
|
+
|
|
193
|
+
Next.js 15 changed page-level `searchParams` to a Promise. The library silently decodes nothing from an unresolved Promise.
|
|
194
|
+
|
|
195
|
+
Source: Next.js 15 migration notes
|
|
196
|
+
|
|
197
|
+
### HIGH Using `state-in-url/next` in a Pages Router project
|
|
198
|
+
|
|
199
|
+
Wrong:
|
|
200
|
+
|
|
201
|
+
```typescript
|
|
202
|
+
// pages/index.tsx
|
|
203
|
+
import { useUrlState } from 'state-in-url/next'; // imports next/navigation
|
|
204
|
+
```
|
|
205
|
+
|
|
206
|
+
Correct:
|
|
207
|
+
|
|
208
|
+
Use App Router (`app/` directory). The library does not support Pages Router and there are no plans to add support. If you must stay on Pages Router, build a custom hook with `useUrlStateBase` and a `next/router`-backed router object.
|
|
209
|
+
|
|
210
|
+
Source: README "Gotchas" #3
|
|
211
|
+
|
|
212
|
+
### MEDIUM Setting `useHistory: false` unnecessarily
|
|
213
|
+
|
|
214
|
+
Wrong:
|
|
215
|
+
|
|
216
|
+
```typescript
|
|
217
|
+
useUrlState(FORM_STATE, { searchParams, useHistory: false });
|
|
218
|
+
// every keystroke triggers a Next.js _rsc payload fetch
|
|
219
|
+
```
|
|
220
|
+
|
|
221
|
+
Correct:
|
|
222
|
+
|
|
223
|
+
```typescript
|
|
224
|
+
useUrlState(FORM_STATE, { searchParams });
|
|
225
|
+
// useHistory defaults to true → window.history, no server round-trip
|
|
226
|
+
```
|
|
227
|
+
|
|
228
|
+
`useHistory: false` is only correct when the server page must re-fetch on URL changes. For UI-only state (filters, drawers, tabs) it triples request traffic.
|
|
229
|
+
|
|
230
|
+
Source: JSDoc on `useUrlState` params; vercel/next.js#59167
|
|
231
|
+
|
|
232
|
+
### MEDIUM Trying to read `searchParams` in a server layout directly
|
|
233
|
+
|
|
234
|
+
Wrong:
|
|
235
|
+
|
|
236
|
+
```typescript
|
|
237
|
+
export default function Layout({ children, searchParams }: any) {
|
|
238
|
+
// searchParams is always undefined in layouts
|
|
239
|
+
}
|
|
240
|
+
```
|
|
241
|
+
|
|
242
|
+
Correct:
|
|
243
|
+
|
|
244
|
+
Use the Proxy + header pattern shown above (`proxy.ts` setting `searchParams` header → layout reads via `headers()` and `decodeState`). With this in place there is no initial lag or extra rerender.
|
|
245
|
+
|
|
246
|
+
Source: README "Using hook in layout component"; packages/example-nextjs16/src/middleware.ts; maintainer interview
|
|
247
|
+
|
|
248
|
+
## See also
|
|
249
|
+
|
|
250
|
+
- `state-in-url/feature-state-hook` — base pattern; required reading.
|
|
251
|
+
- `state-in-url/input-handling` — for text-input UX with the Next.js hook.
|
|
@@ -0,0 +1,180 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: react-router-remix-setup
|
|
3
|
+
description: >
|
|
4
|
+
Set up useUrlState for React Router v7 (state-in-url/react-router), React
|
|
5
|
+
Router v6 (state-in-url/react-router6 — moved here in 6.0.0), and Remix v2
|
|
6
|
+
(state-in-url/remix). The hook API is identical across all three; only the
|
|
7
|
+
import path and per-call NavigateOptions (preventScrollReset, state, replace)
|
|
8
|
+
differ. Load this skill when wiring useUrlState into any non-Next.js
|
|
9
|
+
React router or migrating from an older import path.
|
|
10
|
+
type: framework
|
|
11
|
+
library: state-in-url
|
|
12
|
+
framework: react
|
|
13
|
+
library_version: '6.1.3'
|
|
14
|
+
requires:
|
|
15
|
+
- feature-state-hook
|
|
16
|
+
sources:
|
|
17
|
+
- 'asmyshlyaev177/state-in-url:packages/urlstate/react-router/useUrlState/'
|
|
18
|
+
- 'asmyshlyaev177/state-in-url:packages/urlstate/react-router6/useUrlState/'
|
|
19
|
+
- 'asmyshlyaev177/state-in-url:packages/urlstate/remix/useUrlState/'
|
|
20
|
+
- 'asmyshlyaev177/state-in-url:CHANGELOG.md#600-2025-08-05'
|
|
21
|
+
---
|
|
22
|
+
|
|
23
|
+
This skill builds on `state-in-url/feature-state-hook`. Read it first for the module-scoped default-state rule.
|
|
24
|
+
|
|
25
|
+
# state-in-url — React Router and Remix
|
|
26
|
+
|
|
27
|
+
The hook signature, return shape, and behavior of `useUrlState` are identical across React Router v6, v7, and Remix v2. Three things differ:
|
|
28
|
+
|
|
29
|
+
| Aspect | Difference |
|
|
30
|
+
|---|---|
|
|
31
|
+
| Import path | `state-in-url/react-router` (v7), `state-in-url/react-router6` (v6), `state-in-url/remix` (Remix v2) |
|
|
32
|
+
| `setUrl` per-call options | Accepts `NavigateOptions` from `react-router` (e.g. `preventScrollReset`, `state`, `replace`) |
|
|
33
|
+
| SSR | Remix v2 routes can hydrate from loader data; React Router CSR-only by default |
|
|
34
|
+
|
|
35
|
+
## Setup
|
|
36
|
+
|
|
37
|
+
### React Router v7
|
|
38
|
+
|
|
39
|
+
```typescript
|
|
40
|
+
import { useUrlState } from 'state-in-url/react-router';
|
|
41
|
+
|
|
42
|
+
type FiltersState = { sort: 'name' | 'date'; page: number };
|
|
43
|
+
const FILTERS_STATE: FiltersState = { sort: 'name', page: 1 };
|
|
44
|
+
|
|
45
|
+
export function FiltersBar() {
|
|
46
|
+
const { urlState, setUrl } = useUrlState(FILTERS_STATE);
|
|
47
|
+
return <button onClick={() => setUrl({ page: urlState.page + 1 })}>Next</button>;
|
|
48
|
+
}
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
### React Router v6 (since state-in-url 6.0.0)
|
|
52
|
+
|
|
53
|
+
```typescript
|
|
54
|
+
// Note the /react-router6 subpath
|
|
55
|
+
import { useUrlState } from 'state-in-url/react-router6';
|
|
56
|
+
|
|
57
|
+
type FiltersState = { sort: 'name' | 'date'; page: number };
|
|
58
|
+
const FILTERS_STATE: FiltersState = { sort: 'name', page: 1 };
|
|
59
|
+
|
|
60
|
+
export function FiltersBar() {
|
|
61
|
+
const { urlState, setUrl } = useUrlState(FILTERS_STATE);
|
|
62
|
+
return <button onClick={() => setUrl({ page: urlState.page + 1 })}>Next</button>;
|
|
63
|
+
}
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
### Remix v2
|
|
67
|
+
|
|
68
|
+
```typescript
|
|
69
|
+
import { useUrlState } from 'state-in-url/remix';
|
|
70
|
+
|
|
71
|
+
type FiltersState = { sort: 'name' | 'date'; page: number };
|
|
72
|
+
const FILTERS_STATE: FiltersState = { sort: 'name', page: 1 };
|
|
73
|
+
|
|
74
|
+
export default function Route() {
|
|
75
|
+
const { urlState, setUrl } = useUrlState(FILTERS_STATE);
|
|
76
|
+
return <button onClick={() => setUrl({ page: urlState.page + 1 })}>Next</button>;
|
|
77
|
+
}
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
## Core Patterns
|
|
81
|
+
|
|
82
|
+
### Pass NavigateOptions per call
|
|
83
|
+
|
|
84
|
+
```typescript
|
|
85
|
+
setUrl({ tab: 'b' }, { replace: false, preventScrollReset: true });
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
`replace: false` creates a new history entry (push) instead of replacing.
|
|
89
|
+
`preventScrollReset: true` keeps the page scroll position after the navigation — important when URL state controls in-page UI rather than route content.
|
|
90
|
+
|
|
91
|
+
### `basename` support
|
|
92
|
+
|
|
93
|
+
Both `useUrlState` hooks for React Router respect the router's `basename`. No extra configuration is needed in the hook.
|
|
94
|
+
|
|
95
|
+
```typescript
|
|
96
|
+
// router setup
|
|
97
|
+
<BrowserRouter basename="/admin">
|
|
98
|
+
<App />
|
|
99
|
+
</BrowserRouter>
|
|
100
|
+
// URL state updates happen at /admin?... not /...
|
|
101
|
+
```
|
|
102
|
+
|
|
103
|
+
### Functional update preserving other fields
|
|
104
|
+
|
|
105
|
+
```typescript
|
|
106
|
+
setUrl((curr) => ({ ...curr, page: curr.page + 1 }));
|
|
107
|
+
```
|
|
108
|
+
|
|
109
|
+
## Common Mistakes
|
|
110
|
+
|
|
111
|
+
### CRITICAL Importing `/react-router` for a React Router v6 project
|
|
112
|
+
|
|
113
|
+
Wrong:
|
|
114
|
+
|
|
115
|
+
```typescript
|
|
116
|
+
// react-router v6 project
|
|
117
|
+
import { useUrlState } from 'state-in-url/react-router'; // targets v7 since 6.0.0
|
|
118
|
+
```
|
|
119
|
+
|
|
120
|
+
Correct:
|
|
121
|
+
|
|
122
|
+
```typescript
|
|
123
|
+
import { useUrlState } from 'state-in-url/react-router6';
|
|
124
|
+
```
|
|
125
|
+
|
|
126
|
+
As of state-in-url v6.0.0 (Aug 2025) the v6 implementation moved to its own subpath. `/react-router` now targets React Router v7 only. Agents trained on older code will use the wrong import.
|
|
127
|
+
|
|
128
|
+
Source: CHANGELOG v6.0.0 BREAKING CHANGES (asmyshlyaev177/state-in-url)
|
|
129
|
+
|
|
130
|
+
### HIGH Using the deprecated `useUrlState({ allParamsObj })` signature
|
|
131
|
+
|
|
132
|
+
Wrong:
|
|
133
|
+
|
|
134
|
+
```typescript
|
|
135
|
+
useUrlState({ defaultState: FORM_STATE, replace: true }); // removed in v5.0.0
|
|
136
|
+
```
|
|
137
|
+
|
|
138
|
+
Correct:
|
|
139
|
+
|
|
140
|
+
```typescript
|
|
141
|
+
useUrlState(FORM_STATE, { replace: true });
|
|
142
|
+
```
|
|
143
|
+
|
|
144
|
+
state-in-url v5.0.0 removed the old object-wrapped call shape. Defaults are now passed positionally; options go in the second argument.
|
|
145
|
+
|
|
146
|
+
Source: CHANGELOG v5.0.0 BREAKING CHANGES (asmyshlyaev177/state-in-url)
|
|
147
|
+
|
|
148
|
+
### CRITICAL `defaultState` defined inside the React component
|
|
149
|
+
|
|
150
|
+
(Cross-skill failure — also in `feature-state-hook`.)
|
|
151
|
+
|
|
152
|
+
Wrong:
|
|
153
|
+
|
|
154
|
+
```typescript
|
|
155
|
+
export function Filters() {
|
|
156
|
+
const defaults = { sort: 'name' };
|
|
157
|
+
const { urlState } = useUrlState(defaults);
|
|
158
|
+
}
|
|
159
|
+
```
|
|
160
|
+
|
|
161
|
+
Correct:
|
|
162
|
+
|
|
163
|
+
```typescript
|
|
164
|
+
type FiltersState = { sort: 'name' | 'date' };
|
|
165
|
+
const FILTERS_STATE: FiltersState = { sort: 'name' };
|
|
166
|
+
|
|
167
|
+
export function Filters() {
|
|
168
|
+
const { urlState } = useUrlState(FILTERS_STATE);
|
|
169
|
+
}
|
|
170
|
+
```
|
|
171
|
+
|
|
172
|
+
Same identity rule as Next.js. A fresh default object each render breaks sharing and initial-state seeding.
|
|
173
|
+
|
|
174
|
+
Source: GitHub issues #57, #60, #69 (asmyshlyaev177/state-in-url)
|
|
175
|
+
|
|
176
|
+
## See also
|
|
177
|
+
|
|
178
|
+
- `state-in-url/feature-state-hook` — base pattern.
|
|
179
|
+
- `state-in-url/input-handling` — for text-input UX inside a router-driven page.
|
|
180
|
+
- `state-in-url/form-library-integration` — for pairing with `react-hook-form`.
|