Skip to content

useTypedSearchParams (Next.js)

useTypedSearchParams<T>(options?): [T, setter]

Retrieves and parses the URL search parameters into a typed object, and provides a setter to update them. Backed by Next.js useSearchParams(), usePathname(), and useRouter().

NOTE

Must be used inside a Client Component ("use client").

Generic type parameter

Pass a type argument T extends QueryParams to get a strongly-typed query object and a constrained setter. When T is omitted, the hook falls back to the untyped QueryParams shape.

tsx
"use client";

import { useTypedSearchParams } from "react-routes-forge/next";

type SearchQuery = { q?: string; page?: number; active?: boolean };

function Search() {
  const [query, setQuery] = useTypedSearchParams<SearchQuery>({
    coerceNumbers: true,
    coerceBooleans: true,
  });

  // query.q is typed as string | undefined
  // query.page is typed as number | undefined
  return (
    <input
      value={query.q ?? ""}
      onChange={(e) => setQuery({ ...query, q: e.target.value })}
    />
  );
}

Options

OptionDescription
coerceBooleansConvert "true"/"false" strings to real booleans
coerceNumbersConvert numeric strings to numbers

Setter options

The setter accepts an optional second argument for navigation behavior:

tsx
// Use router.replace to avoid adding a history entry
setQuery({ page: 2 }, { replace: true });

// Control scroll restoration
setQuery({ page: 2 }, { scroll: false });
OptionTypeDescription
replacebooleanUses router.replace instead of router.push
scrollbooleanControls Next.js scroll behavior after navigation

See also

Released under the MIT License.