Overview
useQueryStates is a React hook that synchronizes multiple URL query parameters with component state. It’s ideal for managing related query parameters that should always move together, providing atomic updates and type-safe access to multiple state values.
Function Signature
Type Definitions
Parameters
UseQueryStatesKeysMap
required
An object describing the keys to synchronize and how to parse and serialize them.Each key in the object represents a query parameter, with a parser configuration as its value.
UseQueryStatesOptions<KeyMap>
Optional configuration object for behavior options.
Behavior Options
'push' | 'replace'
default:"'replace'"
How query updates affect page history:
'replace': Keep the current history point (default)'push': Create a new history entry
boolean
default:false
Whether to scroll to top after a query state update.
boolean
default:true
Client-side only updates when
true. Set to false to trigger server re-renders (Next.js only).number
default:50
Maximum time (ms) to wait between URL updates.
LimitUrlUpdates
Rate limiting configuration for URL updates:
TransitionStartFunction
Pass
startTransition from React.useTransition() to observe loading states.boolean
default:true
Clear query parameters from the URL when setting to default values.
UrlKeys<KeyMap>
Map state keys to different URL parameter names:
Return Value
Returns a tuple[state, setState] similar to React.useState:
Values<KeyMap>
An object containing all state values with keys matching the Values are
keyMap:null if not in URL and no default is provided, otherwise the parsed or default value.SetValues<KeyMap>
State updater function accepting:
-
Partial object with new values:
-
Updater function receiving old state:
-
null to clear all keys:
values: Partial object, updater function, ornulloptions: Optional options to override hook-level settings
Usage Examples
Basic Multi-State Management
Partial Updates
With Updater Function
With URL Key Mapping
Complex Filter State
Sharing Parser Definitions
With History Push
With Transitions (Server Updates)
Awaiting Batched Updates
Behavior Notes
Atomic updates: All state changes in a single
setState call are applied to the URL together, ensuring consistency.Partial updates: You can update a subset of keys. Omitted keys retain their current values.
Clearing all state: Passing
null to setState clears all managed query parameters from the URL.Default values: Keys with default values will never be
null in the returned state object.Cross-hook synchronization: Multiple
useQueryState or useQueryStates hooks managing the same URL parameters will stay synchronized automatically.Common Patterns
Conditional Updates
Reset Individual Keys
Override Options Per Update
Comparison with useQueryState
See Also
- useQueryState - Manage a single query parameter
- Built-in Parsers - Ready-to-use parsers for common types
- createSerializer - Generate query strings for links
- createSearchParamsCache - Server-side parameter access