Make sure you’ve completed the Installation steps before continuing.
Basic Usage: Search Input
Let’s create a search component that stores the query in the URL.1
Create your component
Create a new client component (for Next.js app router, mark it with The
'use client'):components/Search.tsx
useQueryState hook works just like useState, but syncs with the URL query string!2
Try it out
Add the component to your page and start typing. Watch the URL update in real-time:
- Type “react” → URL becomes
?q=react - Click “Clear” → URL becomes
/(query removed) - Refresh the page → Your input persists!
Adding Type Safety with Parsers
Let’s add pagination with proper number parsing:1
Import parsers
nuqs provides built-in parsers for common types:
components/Pagination.tsx
Setting
page to 1 (the default value) will clear the query from the URL automatically.2
Benefits of parsers
- Type safety:
pageis always a number, nevernull - Validation: Invalid values are rejected and fall back to the default
- Serialization: Numbers are properly converted to/from URL strings
Managing Multiple Query States
UseuseQueryStates to manage related queries together:
Next Steps
You now know the basics of nuqs! Here’s what to explore next:Parsers
Learn about all built-in parsers and how to create custom ones
Options
Control history mode, shallow routing, and throttling
Server-Side
Use nuqs with Server Components and server-side rendering
Testing
Learn how to test components that use nuqs
Common Patterns
Debouncing Input
For search inputs, you might want to debounce URL updates:History Mode
By default, URL updates replace the current history entry. To enable Back button navigation:Server Updates (Next.js)
By default, updates are client-only (shallow). To trigger server component re-renders:Complete Example
Here’s a complete example combining everything:app/page.tsx
?search=laptop&page=2&limit=25