A React hook to safely and easily read from URL search params.
Docs for version 1.4.4 (the last version before version 2.0.0) can be viewed here
Docs for version 2.3.0 (the last version before being deprecated) can be viewed here
As an alternative, use use-search-param-state
:
import { useSearchParamState } from "use-search-param-state";
function Demo() {
const [counter] = useSearchParamState("c", 0);
}
All of the options from use-search-param
:
interface Options<TVal> {
sanitize?: (unsanitized: string) => string;
parse?: (unparsed: string) => TVal;
validate?: (unvalidated: unknown) => TVal | null;
onError?: (error: unknown) => void;
serverSideSearchParams?: string;
}
are still supported, though note that serverSideSearchParams
is updated to serverSideURLSearchParams: URLSearchParams
in use-search-param-state
Prior to use-search-param-state
v3.0.0, useSearchParamState
required being wrapped in a context provider: SearchParamStateProvider
. For cases where setting the state wasn't necessary, use-search-param
provided a simpler alternative with the same API - but without a provider wrapping your app.
In v3.0.0, use-search-param-state
was refactored to remove the need for a context provider. Without a provider, there's no longer a significant difference between use-search-param-state
and use-search-param
, and no reason to maintain both libraries.