Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

wouter-search

guoyunhe265MIT1.3.0TypeScript support: included

Provide useSearchParams(), useSearchParam() and useSearchParamJson() hooks for wouter

react, react-router, wouter, search-params

readme

wouter-search

Version Downloads Bundle size

Provide useSearchParams(), useSearchParam() and useSearchParamJson() hooks for wouter.

This project is based on junwen-k's initial work.

Note:

  • Require wouter 3.x
  • Require React 16.8+. Preact should work via preact/compat.

Installation

npm install --save wouter-search

Hooks

useSearchParams()

Allow you to get and set all search parameters. The API is similar but not the same to react-router's. The first returned value is a URLSearchParams object and the second returned value is a setter that accepts a URLSearchParams object with options.

import { useSearchParams } from 'wouter-search';

function MyPage() {
  const [searchParams, setSearchParams] = useSearchParams();

  return (
    <button
      onClick={() => {
        // push new history entry
        setSearchParams((prev) => {
          prev.set('foo', 'bar');
          return prev;
        });
        // or replace history entry
        setSearchParams(
          (prev) => {
            prev.set('foo', 'bar');
            return prev;
          },
          {
            replace: true,
          },
        );
      }}
    >
      foobar
    </button>
  );
}

useSearchParam()

A simple wrapper around useSearchParams. By giving a key/name, you can control a specific search parameter straight-forward.

import { useSearchParam, useSearchParams } from 'wouter-search';

function MyPage() {
  const [searchParams, setSearchParams] = useSearchParams();
  const [query, setQuery] = useSearchParam('query', searchParams, setSearchParams);

  return <input onChange={(e) => setQuery(e.target.value, { replace: true })} />;
}

useSearchParamJson()

If your search parameter contains JSON data, useSearchParamJson() will make your life much easier!

import { useSearchParamJson, useSearchParams } from 'wouter-search';

function MyPage() {
  const [searchParams, setSearchParams] = useSearchParams();
  const [solution, setSolution] = useSearchParamJson('solution', searchParams, setSearchParams);

  return (
    <div>
      <h1>{solution.name}</h1>
      <p>{solution.description}</p>
    </div>
  );
}

changelog

Changelog

1.3.0 - 2025-01-25

  • Deprecated useSearchParams, SetSearchParams and URLSearchParamsInit, which have been included in new version of wouter

1.2.1 - 2025-01-16

  • Fixed location path lost issue

1.2.0 - 2024-12-25

  • Changed to ESM-only package

1.1.0 - 2024-12-25

  • Added useSearchParamJson() hook

1.0.2 - 2024-12-24

  • Fixed multiple useSearchParam override each other

1.0.1 - 2024-12-24

  • Fixed searchParams update when calling setSearchParams() multiple times

1.0.0 - 2024-12-24

🌲 Merry Christmas! 🎅

  • Added useSearchParams() hook
  • Added useSearchParam() hook