Skip to main content

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.
Deprecated: Use limitUrlUpdates instead.
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 keyMap:
Values are null if not in URL and no default is provided, otherwise the parsed or default value.
SetValues<KeyMap>
State updater function accepting:
  1. Partial object with new values:
  2. Updater function receiving old state:
  3. null to clear all keys:
Parameters:
  • values: Partial object, updater function, or null
  • options: Optional options to override hook-level settings
Returns: Promise resolving with updated URLSearchParams

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.
URL key mapping: When using urlKeys, always access state using the state key names (from keyMap), not the URL parameter names.
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