Skip to content
WunderUI

useDataTableUrlState

Pro

Keeps the page, sorting, filters and search of a DataTable in the address bar.

import { useDataTableUrlState } from "@wunderui/react"

Playground

Installation

useDataTableUrlState comes with WunderUI Core and Pro, in the entry of the package from your plan. See the plans. Then import it:

tsx
import { useDataTableUrlState } from "@wunderui/react"

When to use

Use useDataTableUrlState for

  • A table whose view people link, reload or share: a filtered, sorted page of orders, a search result.
  • A page that should open the way it was left after a reload.
  • Several tables on one page that each keep their own view (a prefix per table).
  • Server-side data: the same parameters tell the server what to fetch.

Use something else for

  • Data Table for the table itself. Its and props take what this hook returns.
  • Controlled props for keeping the query somewhere else, such as a store or a router. Pass , , and with their callbacks.
  • serializeDataTableQuery for building a link to a prepared view (a dashboard tile that opens the overdue orders). It returns the query string for a ; reads one back.
  • No hook for a table nobody links to. The URL adds nothing there.

Signature

Spread the result into a : the table starts from the query it finds in the URL, and every change rewrites the URL with . A reload or a shared link opens the same view, and the back button still leaves the page instead of undoing a sort.

ts
useDataTableUrlState(options?: DataTableUrlStateOptions & { search?: string; }): { initialState: DataTableQuery; onQueryChange: (query: DataTableQuery) => void; }

Parameters

ParameterTypeDefaultDescription
options optionalDataTableUrlStateOptions & { search?: string; }{}Optional. puts a namespace in front of the parameter names ( gives ), so two tables on a page do not share parameters. is what an untouched table shows (, , , , each optional): parameters equal to it are left out of the URL. The page size defaults to 10. is a query string to start from instead of , for server-rendered pages. All three are read when the table starts; and are also read again whenever the URL is written.

Returns

Two props for . Spread them on the table: is read once, is called after each change.

FieldTypeDescription
initialStateDataTableQueryThe query the table starts from: the defaults, with whatever the URL (or ) contained on top. Computed once on the first render and kept. Hand it to as .
onQueryChange(query: DataTableQuery) => voidWrites the table's query into the URL. calls it after every change of page, order, filters or search (not on its first render). It keeps its identity and does nothing on the server. With server-side data, fetch with the query it receives.

Behavior and motion

The URL is read once, at the start, and written after every change. In between the hook listens to nothing.

Format
is the search text, the order as or pairs separated by commas, the page (1-based), the page size and the column filters as JSON. With a prefix every name starts with it (). Values equal to the defaults, an empty search and empty lists are left out, so an untouched table has a clean URL.
Reading
Only what is present and well-formed is used; anything else is ignored, so a hand-edited or outdated link never breaks the table. A page or size that is not a positive whole number, a size above 1000, a sort direction other than or and filters that are not a JSON list of fall back to the defaults.
Writing
Each change calls with the current history state and the current hash. The path and the other parameters of the URL stay; only this table's own parameters are replaced. As it replaces, the back button leaves the page instead of undoing a sort, and no history entries pile up. The table's first render writes nothing.
Defaults you remove
A cleared sorting or a removed filter is written as no parameter, because an empty list is never written. If your sort the table, a reload brings the default sort back instead of the unsorted table the person had.
Server rendering
On the server there is no address bar, so the table starts from the defaults. In the browser the first render reads , which differs from that HTML as soon as the URL has parameters, and React reports a mismatch. On a server-rendered page pass the request's query string as ; it replaces for the first render. Alternatively mount the table after hydration, as the demos here do.
Changes from outside
The URL is not watched. A link to the same page with other parameters, or a script that changes the URL, does not move the table; mount it again (with a ) to read the URL anew.
Cleanup
Nothing to clean up: no listeners, no timers. The parameters stay in the URL when the table goes away.
Motion
The hook has no motion, so there is nothing for reduced motion to change.

Best practices

  • Do

    Give every table of a page its own , such as . Without one, two tables read and write the same and .

  • Do

    Put the starting view in . A table that opens sorted or on a smaller page then still has a clean URL.

  • Do

    On a server-rendered page pass the request's query string as , so the first render matches the server's HTML.

  • Do

    Keep and stable, as asks. Column ids must match the ids in the URL, so a renamed column ignores old links.

  • Do

    With server-side data fetch with the query hands you. Then table and data come from the same parameters.

  • Don’t

    Do not rely on the back button to undo a sort or a filter. The hook replaces the URL entry on purpose.

  • Don’t

    Do not put anything private in a filter. The URL shows up in history, logs and shared links.

  • Don’t

    Do not use it for a table nobody links to or reloads. It only adds a changing URL.

Notes for AI

Give your coding assistant the page or the MCP call instead of screenshots.

MCPget_component("useDataTableUrlState")
Importimport { useDataTableUrlState } from "@wunderui/react"

The full library index for assistants is at /llms.txt.