Overview
useQueryState is a React hook that synchronizes a single URL query parameter with component state. It works like useState, but the state is stored in the URL query string and persists across page reloads and browser navigation.
Function Signature
Type Definitions
Parameters
The URL query string key to bind to. This will be the parameter name in the URL.Example:
'tag' will manage the tag parameter in ?tag=valueConfiguration object combining parser options and behavior options.
Parser Options
Function to convert a query string value into a state value. Return
null for invalid inputs.Function to render the state value into a query string value. Defaults to
String().Default value to return when the query parameter is not present in the URL. Makes the returned state non-nullable.
Equality comparison function. Used with
clearOnDefault to determine if a value equals the default.Behavior Options
How the query update affects page history:
'replace': Keep the current history point and only replace the query string (default)'push': Create a new history entry, allowing use of back/forward buttons
Whether to scroll to top after a query state update.
Client-side only updates when
true (default). Set to false to trigger server re-renders (Next.js only).Maximum time (ms) to wait between URL updates. Minimum value is 50ms. Safari may require ~120ms.
Rate limiting configuration for URL updates:
Pass
startTransition from React.useTransition() to observe loading states during server updates.Clear the query parameter from the URL when setting state to the default value.
Return Value
Returns a tuple[state, setState] similar to React.useState:
The current state value:
nullif the query parameter is not in the URL (when no default value is provided)- The parsed value from the URL
- The default value if provided and query parameter is missing
State updater function with the signature:Parameters:
value: New state value,nullto clear, or updater functionoptions: Optional options to override hook-level settings
Usage Examples
Basic String State
With Type Parser
With Default Value
With History Push
With Custom Parser
With Transitions (Server Updates)
Awaiting URL Updates
Behavior Notes
Default values are internal: When you specify a
defaultValue, it will be returned when the query parameter is missing, but it won’t be written to the URL.Clearing state: Setting the state to
null removes the key from the query string. If a default value is provided, the state will return to that default.State updates are batched: Multiple
setState calls in the same event loop tick are merged and applied to the URL together.URL updates are throttled: To prevent browser rate-limiting of the History API, URL updates are throttled (default: 50ms). The returned state updates immediately for responsive UI.
Common Patterns
Override Options Per Update
Builder Pattern Configuration
See Also
- useQueryStates - Manage multiple related query parameters together
- Built-in Parsers - Ready-to-use parsers for common types
- createParser - Create custom parsers with type safety