useSearchParams
Reads the query of the current location and sets keys of it.
Reads the query of the current location and sets keys of it. validate turns the parsed query into
a typed value (it may throw to reject a query; the throw is the caller's to handle). A setter merges
its keys into the query, a key set to undefined is removed, and the setters called in one tick
become one navigation: the last value of a key wins, and the entry is replaced only when every call
in the batch asked for it. A functional update reads the value at the time of the call, not after the
batch.
Import#
ts
import { useSearchParams } from "@shadow-js/router";
Signature#
ts
function useSearchParams<S = SearchRecord>(
validate?: (search: SearchRecord) => S,
): readonly [Accessor<S>, (next: SearchUpdate<S>, options?: SearchSetOptions) => void]
Parameters#
| Name | Description |
|---|---|
validate |
Types the query. Default: the parsed query, unchanged. |
Returns#
The read accessor and the setter.
Examples#
ts
const [params, setParams] = useSearchParams((search) => ({ page: Number(search.page ?? 1) }));
setParams({ page: params().page + 1 }); // pushes ?page=2