useDataTableUrlState
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:
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.
useDataTableUrlState(options?: DataTableUrlStateOptions & { search?: string; }): { initialState: DataTableQuery; onQueryChange: (query: DataTableQuery) => void; }Parameters
Returns
Two props for . Spread them on the table: is read once, is called after each change.
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.
get_component("useDataTableUrlState")import { useDataTableUrlState } from "@wunderui/react"The full library index for assistants is at /llms.txt.