Skip to content
@pyreon/url-state
fundamentals · url

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
Documentation
one line
useUrlState('page', 1)?page=3 reads 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.
A signal that lives in the address bar Schematic: a browser address bar reads /products?q=lamp. Clicking Next sets the page signal to 2 and the URL becomes /products?q=lamp&page=2. The back button restores the URL, and the page signal reads 1 again; the default value never appears in the URL. USEURLSTATE('PAGE', 1) · TYPED FROM ITS DEFAULT shop.example.com/products?q=lamp shop.example.com/products?q=lamp&page=2 search lamp filters.q() → 'lamp' page 1 2 Next → page() → number, not a string reload · share the link · back button: the state comes with it page 1 is the default, so it never appears in the URL
  • 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 · batchUrlUpdates makes one replaceState

synthetic · author-run · M3 Max · record of 2026-11-05

Every result, including where we lose →
why teams pick it

State the user can bookmark.

  1. Typed by its default

    useUrlState('page', 1) returns a number, useUrlState('inStock', false) a boolean, useUrlState('tags', [] as string[]) an array. ?tags=a,b reads back as ['a', 'b'].

  2. Clean URLs by default

    A value equal to its default leaves the URL, so sort=name never shows up. Set clearOnDefault: false to keep it.

  3. Back and forward just work

    A popstate updates every signal bound to that key and re-runs what reads it. Two components on the same key stay in sync.

  4. One history write per click

    Writes use replaceState unless you ask for push. batchUrlUpdates(() => { … }) folds several params into one history operation; debounceMs: 300 keeps a search box from writing on every key.

the difference

The parsers come from your defaults.

React + nuqs 2

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>
)
with @pyreon/url-state

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

Coming from nuqs?

Parsers become defaults; setters become .set().

nuqs
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'])
Pyreon

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']
try it

Change filters, watch the URL.

Every control is a URL-synced signal. Defaults disappear from the address bar.

/products/urlstate

Page1 / 3
  • 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.

benchmarks

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.

vs nuqs 2.10.2 parsers · bun/JavaScriptCorePyreonrival · lower is better
  • array parse
    win2.1×
  • boolean
    win1.8×
  • float
    win1.7×
  • array serialize
    win1.2×
  • string
    tiecould not distinguish
  • number serialize
    tiecould not distinguish
  • round-trip
    tiecould not distinguish
  • integer parse
    8.1 ns
    7 ns
    lossnuqs 1.1×
Apple M3 Max · record of 2026-11-05 · overnight run, one run · author-run, syntheticFull method, every loss →
what's included

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) or arrayFormat: 'repeat' for ?tags=a&tags=b.
  • clearOnDefault On by default: the URL holds only what differs from the default.
  • debounceMs One URL write after the user stops typing.
  • replace: false Use pushState when a change deserves its own back-button step.
  • batchUrlUpdates Several params, one history operation.
  • onChange Fires on back/forward, router navigation or another hook writing the same key, never for your own .set().
  • setUrlRouter Route writes through @pyreon/router (or anything with replace(path)) so guards and scroll handling stay in charge.
recipes

Things to build with it.

A product list you can link to

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)} />
}
Tabs that survive a reload

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 → billing
api

Three functions.

Every export has a reference page with its signature, an example and the mistakes people make with it.

functions · 3
  • useUrlState
  • batchUrlUpdates
  • setUrlRouter
options · 7
  • debounceMs
  • replace
  • arrayFormat
  • clearOnDefault
  • serialize
  • deserialize
  • onChange
types · 5
  • UrlStateSignal
  • UrlStateOptions
  • ArrayFormat
  • Serializer
  • UrlRouter
good to know

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.

questions

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
Read the URL state guide