Refresh. Paste the link. The filters are still there.
useUrlState('page', 1) is a signal that lives in the address bar, typed from its default. Search, sort, tabs and pagination survive a reload, a shared link and the back button, with no parsers to write and no adapter to mount.
bun add @pyreon/url-state - one line
useUrlState('page', 1)?page=3reads back as the number 3.- types
- from the defaultNumbers, booleans, strings and arrays; custom serializers for the rest.
- vs nuqs 2
- array parse 2.1× fasterOne run, bun/JavaScriptCore. Integer parse is 1.1× slower.
- where it runs
- webOn iOS and Android (experimental), scalar params bind to the native router's query instead; literal arrays follow in the next release.
- 2.1× faster array parsing than nuqs 2BENCHMARKS.md §10, url-state row · overnight run, one run, bun/JavaScriptCore
- 1.8× faster boolean parsing than nuqsBENCHMARKS.md §10, url-state row · one run
- 0 new history entries for a batch of writes made with the default
replace.scratch/app/url.ts ·batchUrlUpdatesmakes onereplaceState
synthetic · author-run · M3 Max · record of 2026-11-05
Every result, including where we lose →State the user can bookmark.
Typed by its default
useUrlState('page', 1)returns a number,useUrlState('inStock', false)a boolean,useUrlState('tags', [] as string[])an array.?tags=a,breads back as['a', 'b'].Clean URLs by default
A value equal to its default leaves the URL, so
sort=namenever shows up. SetclearOnDefault: falseto keep it.Back and forward just work
A
popstateupdates every signal bound to that key and re-runs what reads it. Two components on the same key stay in sync.One history write per click
Writes use
replaceStateunless you ask forpush.batchUrlUpdates(() => { … })folds several params into one history operation;debounceMs: 300keeps a search box from writing on every key.
The parsers come from your defaults.
A parser per param, a tuple per hook, and an adapter at the root.
import { useQueryState, useQueryStates, parseAsInteger,
parseAsString, parseAsBoolean } from 'nuqs'
import { NuqsAdapter } from 'nuqs/adapters/react'
function Products() {
const [page, setPage] = useQueryState('page', parseAsInteger.withDefault(1))
const [filters, setFilters] = useQueryStates({
q: parseAsString.withDefault(''),
sort: parseAsString.withDefault('name'),
inStock: parseAsBoolean.withDefault(false),
})
return (
<>
<input value={filters.q} onChange={(e) => setFilters({ q: e.target.value })} />
<button onClick={() => setPage((p) => p + 1)}>Next ({page})</button>
</>
)
}
export const App = () => (
<NuqsAdapter>
<Products />
</NuqsAdapter>
)Each param is a signal. The default says the type. Only the text that reads it updates.
import { useUrlState } from '@pyreon/url-state'
const Products = () => {
const page = useUrlState('page', 1)
const filters = useUrlState({ q: '', sort: 'name', inStock: false })
return (
<>
<input value={filters.q()} onInput={(e) => filters.q.set(e.currentTarget.value)} />
<button onClick={() => page.set(page() + 1)}>Next ({page()})</button>
</>
)
}Coming from nuqs?
Parsers become defaults; setters become .set().
import { debounce, parseAsArrayOf, parseAsString, useQueryState } from 'nuqs'
const [tags, setTags] = useQueryState(
'tags',
parseAsArrayOf(parseAsString).withDefault([]),
)
const [q, setQ] = useQueryState(
'q',
parseAsString.withDefault('').withOptions({ limitUrlUpdates: debounce(300) }),
)
setTags(['red', 'blue'])nuqs ships more built-in parsers (dates, string literals, enums). Here those are a serialize/deserialize pair on the option, and a plain object default goes through JSON on its own. Both libraries drop a param that equals its default.
import { useUrlState } from '@pyreon/url-state'
const tags = useUrlState('tags', [] as string[])
const q = useUrlState('q', '', { debounceMs: 300 })
tags.set(['red', 'blue']) // ?tags=red%2Cblue
tags() // ['red', 'blue']Change filters, watch the URL.
Every control is a URL-synced signal. Defaults disappear from the address bar.
/products/urlstate
- Arc floor lamp★ 4.6$189
- Beam desk lamp★ 4.2$79
- Cotton throw★ 4.8$59
Reload the page: the filters come back from the URL.
const q = useUrlState('shop-q', '', { debounceMs: 300 })
const sort = useUrlState('shop-sort', 'name')
const inStock = useUrlState('shop-stock', false)
const page = useUrlState('shop-page', 1)- search edits0
- URL writes (replaceState)0
- params in the URL0
Each write uses replaceState, so Back leaves the page instead of stepping through every filter. Defaults never appear in the address bar.
Array parsing 2.1× faster than nuqs.
The codec work that runs on every read and write: parse the param, serialize it back. Wins on booleans, floats and arrays; integer parse is a nanosecond behind; three rows tie.
- array parsewin2.1×
- booleanwin1.8×
- floatwin1.7×
- array serializewin1.2×
- stringtiecould not distinguish
- number serializetiecould not distinguish
- round-triptiecould not distinguish
- integer parse8.1 ns7 nslossnuqs 1.1×
Small API, the whole job.
- Single-param mode
useUrlState('page', 1)with.set(),.reset()and.remove(). - Schema mode
useUrlState({ q: '', sort: 'name' })returns one signal per key. - Type coercion Numbers, floats, booleans and arrays come back as their types, not strings.
- Arrays two ways
?tags=a,b(default, commas inside a value escaped) orarrayFormat: 'repeat'for?tags=a&tags=b. clearOnDefaultOn by default: the URL holds only what differs from the default.debounceMsOne URL write after the user stops typing.replace: falseUsepushStatewhen a change deserves its own back-button step.batchUrlUpdatesSeveral params, one history operation.onChangeFires on back/forward, router navigation or another hook writing the same key, never for your own.set().setUrlRouterRoute writes through@pyreon/router(or anything withreplace(path)) so guards and scroll handling stay in charge.
Things to build with it.
Search, sort, a stock filter and the page number, all in the URL. Changing the search resets the page in the same history write.
import { batchUrlUpdates, useUrlState } from '@pyreon/url-state'
const Catalogue = () => {
const f = useUrlState({ q: '', sort: 'name', inStock: false })
const page = useUrlState('page', 1)
const search = (q: string) =>
batchUrlUpdates(() => {
f.q.set(q)
page.reset() // one replaceState for both
})
return <input value={f.q()} onInput={(e) => search(e.currentTarget.value)} />
}A tab is a string param. The default tab keeps the URL clean; every other tab is shareable.
const tab = useUrlState('tab', 'overview')
const Tabs = () => (
<nav>
<button aria-pressed={() => tab() === 'overview'} onClick={() => tab.set('overview')}>Overview</button>
<button aria-pressed={() => tab() === 'billing'} onClick={() => tab.set('billing')}>Billing</button>
</nav>
)
// /settings → overview
// /settings?tab=billing → billingThree functions.
Every export has a reference page with its signature, an example and the mistakes people make with it.
- useUrlState
- batchUrlUpdates
- setUrlRouter
- debounceMs
- replace
- arrayFormat
- clearOnDefault
- serialize
- deserialize
- onChange
- UrlStateSignal
- UrlStateOptions
- ArrayFormat
- Serializer
- UrlRouter
Good to know before you start.
The server renders the default
On the server every signal starts at its default; it does not read the request URL. A server-rendered page linked with ?page=3 shows page 1 until the client takes over, so read the param in your loader when the first paint must match.
With a router set, the router writes
After setUrlRouter(router), a write is router.replace(url), or router.push(url) for a signal with replace: false. A router without push falls back to replace and warns once.
Complex values need a serializer
Dates and enums take a serialize/deserialize pair; a plain object default is stored as JSON. Keep the URL for what a user would share; put the rest in a store.
The address bar is a web thing
On iOS and Android (experimental), useUrlState with a literal key and a string, number or boolean default binds to the native router's query; literal-array defaults join them in the next release. An object default stays web with a named warning, and debounceMs, clearOnDefault: false and custom serializers have no effect there.
What people ask first.
Does every keystroke add a history entry?
No. Writes default to replaceState, and debounceMs delays the write until typing stops. Pass replace: false only where the back button should step through changes.
What if the URL holds garbage, like ?page=abc?
A value that does not parse as the default's type falls back to the default: ?page=abc reads as 1, ?inStock=yes as false. For stricter rules, pass a deserialize.
Is it faster than nuqs?
On parsing, mostly: boolean 1.8×, array parse 2.1×, array serialize 1.2×, float 1.7×. Integer parse is 1.1× slower (8.1 vs 7.0 ns), and string, number serialize and round-trip tie. One run, bun, nanoseconds either way.
Do I need a router?
No. Without one it writes to history directly. With @pyreon/router, call setUrlRouter(router) once so router navigation and URL state stay in step.
Your users already know how to share a URL. Let them.
bun add @pyreon/url-state