Baike.dev
All toolsOnline toolsAI codingTrendingOpen sourceNewsSubmit
Log in
< Back to tools
N

nuqs

> 前端框架
Open source

Type-safe search params state manager for React frameworks - Like useState, but stored in the URL query string.

10.7K stars0 likes2 views
WebsiteGitHub

About

Type-safe search params state manager for React frameworks - Like useState, but stored in the URL query string.


Type-safe search params state manager for React frameworks. Like useState, but stored in the URL query string.

Features

  • new: Supports Next.js (app and pages routers), plain React (SPA), Remix, React Router, TanStack Router, and custom routers via adapters
  • ‍♀️ Simple: the URL is the source of truth
  • Replace history or append to use the Back button to navigate state updates
  • ⚡️ Built-in parsers for common state types (integer, float, boolean, Date, and more). Create your own parsers for custom types & pretty URLs
  • ♊️ Related querystrings with useQueryStates
  • Shallow mode by default for URL query updates, opt-in to notify server components
  • Server cache for type-safe searchParams access in nested server components
  • ⌛️ Support for useTransition to get loading states on server updates

Documentation

Read the complete documentation at nuqs.dev.

Installation

bash
npm install nuqs
bash
pnpm add nuqs
bash
yarn add nuqs
bash
bun add nuqs
bash
deno add nuqs
bash
vlt install nuqs

Adapters

You will need to wrap your React component tree with an adapter for your framework. (expand the appropriate section below)

▲ Next.js (app router)

Supported Next.js versions: >=14.2.0. For older versions, install nuqs@^1 (which doesn't need this adapter code).

typescript
// src/app/layout.tsx
import { NuqsAdapter } from 'nuqs/adapters/next/app'
import { type ReactNode } from 'react'

export default function RootLayout({ children }: { children: ReactNode }) {
  return (
    <html>
      <body>
        <NuqsAdapter>{children}</NuqsAdapter>
      </body>
    </html>
  )
}
▲ Next.js (pages router)

Supported Next.js versions: >=14.2.0. For older versions, install nuqs@^1 (which doesn't need this adapter code).

typescript
// src/pages/_app.tsx
import type { AppProps } from 'next/app'
import { NuqsAdapter } from 'nuqs/adapters/next/pages'

export default function MyApp({ Component, pageProps }: AppProps) {
  return (
    <NuqsAdapter>
      <Component {...pageProps} />
    </NuqsAdapter>
  )
}
⚛️ Plain React (SPA)

Example: via Vite or create-react-app.

typescript
import { NuqsAdapter } from 'nuqs/adapters/react'

createRoot(document.getElementById('root')!).render(
  <NuqsAdapter>
    <App />
  </NuqsAdapter>
)
Remix

Supported Remix versions: @remix-run/react@>=2

typescript
// app/root.tsx
import { NuqsAdapter } from 'nuqs/adapters/remix'

// ...

export default function App() {
  return (
    <NuqsAdapter>
      <Outlet />
    </NuqsAdapter>
  )
}
React Router v6

Supported React Router versions: react-router-dom@^6

typescript
import { NuqsAdapter } from 'nuqs/adapters/react-router/v6'
import { createBrowserRouter, RouterProvider } from 'react-router-dom'
import App from './App'

const router = createBrowserRouter([
  {
    path: '/',
    element: <App />
  }
])

export function ReactRouter() {
  return (
    <NuqsAdapter>
      <RouterProvider router={router} />
    </NuqsAdapter>
  )
}
React Router v7

Supported React Router versions: react-router@^7

typescript
// app/root.tsx
import { NuqsAdapter } from 'nuqs/adapters/react-router/v7'
import { Outlet } from 'react-router'

// ...

export default function App() {
  return (
    <NuqsAdapter>
      <Outlet />
    </NuqsAdapter>
  )
}
React Router v8

Supported React Router versions: react-router@^8

typescript
// app/root.tsx
import { NuqsAdapter } from 'nuqs/adapters/react-router/v8'
import { Outlet } from 'react-router'

// ...

export default function App() {
  return (
    <NuqsAdapter>
      <Outlet />
    </NuqsAdapter>
  )
}
️ TanStack Router

Supported TanStack Router versions: @tanstack/react-router@^1 Note: TanStack Router support is experimental and does not yet cover TanStack Start.

typescript
// src/routes/__root.tsx
import { NuqsAdapter } from 'nuqs/adapters/tanstack-router'
import { Outlet, createRootRoute } from '@tanstack/react-router'

export const Route = createRootRoute({
  component: () => (
    <>
      <NuqsAdapter>
        <Outlet />
      </NuqsAdapter>
    </>
  )
})

Usage

typescript
'use client' // Only works in client components

import { useQueryState } from 'nuqs'

export default () => {
  const [name, setName] = useQueryState('name')
  return (
    <>
      <h1>Hello, {name || 'anonymous visitor'}!</h1>
      <input value={name || ''} onChange={e => setName(e.target.value)} />
      <button onClick={() => setName(null)}>Clear</button>
    </>
  )
}

useQueryState takes one required argument: the key to use in the query string.

Like React.useState, it returns an array with the value present in the query string as a string (or null if none was found), and a state updater function.

Example outputs for our hello world example:

URL name value Notes
/ null No name key in URL
/?name= '' Empty string
/?name=foo 'foo'
/?name=2 '2' Always returns a string by default, see Parsing below

Parsing

If your state type is not a string, you must pass a parsing function in the second argument object.

We provide parsers for common and more advanced object types:

…

You may pass a custom set of parse and serialize functions:

typescript
import { useQueryState } from 'nuqs'

export default () => {
  const [hex, setHex] = useQueryState('hex', {
    // TypeScript will automatically infer it's a number
    // based on what `parse` returns.
    parse: (query: string) => parseInt(query, 16),
    serialize: value => value.toString(16)
  })
}

Default value

When the query string is not present in the URL, the default behaviour is to return null as state.

It can make state updating and UI rendering tedious. Take this example of a simple counter stored in the URL:

typescript
import { useQueryState, parseAsInteger } from 'nuqs'

export default () => {
  const [count, setCount] = useQueryState('count', parseAsInteger)
  return (
    <>
      <pre>count: {count}</pre>
      <button onClick={() => setCount(0)}>Reset</button>
      {/* handling null values in setCount is annoying: */}
      <button onClick={() => setCount(c => c ?? 0 + 1)}>+</button>
      <button onClick={() => setCount(c => c ?? 0 - 1)}>-</button>
      <button onClick={() => setCount(null)}>Clear</button>
    </>
  )
}

You can specify a default value to be returned in this case:

typescript
const [count, setCount] = useQueryState('count', parseAsInteger.withDefault(0))

const increment = () => setCount(c => c + 1) // c will never be null
const decrement = () => setCount(c => c - 1) // c will never be null
const clearCount = () => setCount(null) // Remove query from the URL

Note: the default value is internal to React, it will not be written to the URL.

Setting the state to null will remove the key in the query string and set the state to the default value.

Options

History

By default, state updates are done by replacing the current history entry with the updated query when state changes.

You can see this as a sort of git squash, where all state-changing operations are merged into a single history value.

You can also opt-in to push a new history item for each state change, per key, which will let you use the Back button to navigate state updates:

typescript
// Default: replace current history with new state
useQueryState('foo', { history: 'replace' })

// Append state changes to history:
useQueryState('foo', { history: 'push' })

Any other value for the history option will fallback to the default.

You can also override the history mode when calling the state updater function:

typescript
const [query, setQuery] = useQueryState('q', { history: 'push' })

// This overrides the hook declaration setting:
setQuery(null, { history: 'replace' })

Shallow

Note: this feature only applies to Next.js

By default, query state updates are done in a client-first manner: there are no network calls to the server.

This is equivalent to the shallow option of the Next.js pages router set to true, or going through the experimental windowHistorySupport flag in the app router.

To opt-in to query updates notifying the server (to re-run getServerSideProps in the pages router and re-render Server Components on the app router), you can set shallow to false:

typescript
const [state, setState] = useQueryState('foo', { shallow: false })

// You can also pass the option on calls to setState:
setState('bar', { shallow: false })

Throttling URL updates

Because of browsers rate-limiting the History API, internal updates to the URL are queued and throttled to a default of 50ms, which seems to satisfy most browsers even when sending high-frequency query updates, like binding to a text input or a slider.

Safari's rate limits are much stricter and would require a throttle of around 340ms. If you end up needing a longer time between updates, you can specify it in the options:

typescript
useQueryState('foo', {
  // Send updates to the server maximum once every second
  shallow: false,
  throttleMs: 1000
})

// You can also pass the option on calls to setState:
setState('bar', { throttleMs: 1000 })

Note: the state returned by the hook is always updated instantly, to keep UI responsive. Only changes to the URL, and server requests when

Issues· 45 open

View all issuesOpen on GitHub
  • #1590

    bug: TanStack Router adapter adds a second query string on routes with a dynamic segment

    Updated Sep 15, 2026
  • #1572

    Add an option for opting out of pretty encoding

    bugUpdated Aug 28, 2026
  • #1563

    react router: `history: 'push'` write with `shallow: false` does not advance the history index

    bugadapters/react-routerfeature/time-safetyUpdated Aug 23, 2026
  • #1531

    Object-returning parsers lose referential equality for values parsed from the URL, causing render loops

    Updated Aug 7, 2026
  • #519

    Who is using `nuqs`?

    internalUpdated Aug 6, 2026
  • #1127

    [bug] TanStack Router adapter: search params with array of objects serialized as [object Object]

    bugadapters/tanstack-routerUpdated Aug 3, 2026
  • #1498

    `useQueryStates` subscriber permanently stuck on a stale value after overlapping `shallow: false` updates when server responses are slow (still on 2.9.1)

    bugUpdated Jul 21, 2026
  • #1404

    Required option for query params

    Updated May 16, 2026
  • #757

    Storybook Issues with v2

    bugUpdated Apr 7, 2026
  • #1114

    AWS API Gateway returns 400 when {} are present in query values

    bugUpdated Mar 18, 2026

> Tags

TypeScriptquery-paramsreactsearch-paramsstate-management

No comments yet. Be the first to share.

> Details

PublishedAug 1, 2026
UpdatedSep 29, 2026
Category前端框架
PricingOpen source

> Related tools

R
React
用于构建用户界面的 JavaScript 库
V
Vue.js
渐进式 JavaScript 框架
N
Next.js
基于 React 的全栈 Web 框架