npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

react-url-search-params

v1.0.3

Published

A zero-dependency, type-safe library for managing URL query parameters in React with schema-based validation. It includes a built-in schema generator, eliminating the need for external validation libraries like Zod or Yup. Query parameter changes automati

Readme

react-url-search-params

A zero-dependency, type-safe library for managing URL query parameters in React with schema-based validation. It includes a built-in schema generator, eliminating the need for external validation libraries like Zod or Yup. Query parameter changes automatically create a new entry in the browser history, ensuring seamless navigation.

Installation

Install the package using npm or yarn or pnpm:

npm install react-url-search-params

or

yarn add react-url-search-params

or

pnpm add react-url-search-params

Features

  • Type-safe URL search parameters handling
  • Default values for query parameters
  • Easy-to-use hooks for managing and updating URL search params
  • validating search params at run-time based on the schema you defined
  • Automatically updates browser's history

Usage

Basic Usage(Typescript)

import {
  useUrlSearchParams,
  createUrlSearchParamsSchema,
  UrlSchemaToType,
} from 'react-url-search-params';

const urlSearchParamsSchema = createUrlSearchParamsSchema({
  q: 'string',
  page: 'number',
  instock: 'boolean',
});

type UrlSearchParams = UrlSchemaToType<typeof urlSearchParamsSchema>;

const Component = () => {
  const { usp } = useUrlSearchParams<UrlSearchParams>(urlSearchParamsSchema);

  return (
    <>
      <input
        type="search"
        onchange={(event: React.ChangeEvent<HTMLInputElement>) => {
          usp.assign({
            q: event.target.value,
          });
        }}
        value={usp.values?.q ?? ''} // You can use || instead of ??(Nullish coalescing operator)
        name="q"
        id="q"
      />
      <button
        onClick={() => {
          usp.append({
            instock: true,
          });
        }}
      >
        In Stock
      </button>
      <div>
        <span>
          <button
            onClick={() => {
              usp.set('page', 1);
            }}
          >
            1
          </button>
        </span>
        <span>
          <button
            onClick={() => {
              usp.set('page', 2);
            }}
          >
            2
          </button>
        </span>
        <span>
          <button
            onClick={() => {
              usp.set('page', 3);
            }}
          >
            3
          </button>
        </span>
      </div>
    </>
  );
};

export default Component;

Basic Usage(Javascript)

import {
  useUrlSearchParams,
  createUrlSearchParamsSchema,
} from 'react-url-search-params';

const urlSearchParamsSchema = createUrlSearchParamsSchema({
  q: 'string',
  page: 'number',
  instock: 'boolean',
});

const Component = () => {
  const { usp } = useUrlSearchParams(urlSearchParamsSchema);

  return (
    <>
      <input
        type="search"
        onchange={(event: React.ChangeEvent<HTMLInputElement>) => {
          usp.assign({
            q: event.target.value,
          });
        }}
        value={usp.values?.q ?? ''} // You can use || instead of ??(Nullish coalescing operator)
        name="q"
        id="q"
      />
      <button
        onClick={() => {
          usp.append({
            instock: true,
          });
        }}
      >
        In Stock
      </button>
      <div>
        <span>
          <button
            onClick={() => {
              usp.set('page', 1);
            }}
          >
            1
          </button>
        </span>
        <span>
          <button
            onClick={() => {
              usp.set('page', 2);
            }}
          >
            2
          </button>
        </span>
        <span>
          <button
            onClick={() => {
              usp.set('page', 3);
            }}
          >
            3
          </button>
        </span>
      </div>
    </>
  );
};

export default Component;

Defining the URL Search Params Schema

const urlSearchParamsSchema = createUrlSearchParamsSchema({
  q: 'string',
  page: 'number',
  instock: 'boolean',
});

Inferring Type from Schema

type UrlSearchParams = UrlSchemaToType<typeof urlSearchParamsSchema>;

Using the useUrlSearchParams Hook

const { usp } = useUrlSearchParams<UrlSearchParams>(urlSearchParamsSchema);

Using the useUrlSearchParams Hook with default value

// This is especially useful when you need to set search parameters immediately upon visiting the page.
// There is no need to use the useEffect hook to initialize search parameters on page load.
const { usp } = useUrlSearchParams<UrlSearchParams>(urlSearchParamsSchema, {
  q: 'hello',
});

Updating Search Params

<input
  type="search"
  onchange={(event: React.ChangeEvent<HTMLInputElement>) => {
    usp.assign({
      q: event.target.value,
    });
  }}
  value={usp.values?.q ?? ''} // You can use || instead of ??(Nullish coalescing operator)
  name="q"
  id="q"
/>

API Reference

createUrlSearchParamsSchema

Creates a schema definition for URL search parameters.

const schema = createUrlSearchParamsSchema({
  key1: 'string',
  key2: 'number',
});

Infer Typescript type from the Schema you defined.

type UrlSearchParams = UrlSchemaToType<typeof schema>;

useUrlSearchParams

Hook to interact with URL search parameters.

const { usp } = useUrlSearchParams<UrlSearchParams>(schema, {
  key1: 'default',
}); // Both the type parameter (<UrlSearchParams>) and the second argument are optional. Use the second argument only if you want to set default values when the page loads with existing search parameters.

usp Methods

usp.toString()

Converts the current URL search parameters to a query string.

const queryString = usp.toString();
console.log(queryString); // Example output: "q=hello&r=3"

usp.has(key: keyof T)

Checks if a search parameter exists.

const exists = usp.has('q');
console.log(exists); // true or false

usp.size()

Gets the number of active search parameters.

const count = usp.size();
console.log(count); // Example output: 2

usp.get(key: keyof T)

Retrieves a search parameter.

const value = usp.get('q');
console.log(value); // Example output: "hello"

usp.getAll()

Retrieves all active search parameters.

const allParams = usp.getAll();
console.log(allParams); // Example output: { q: "hello", r: 3 }

usp.set(key: keyof T, value: UrlSearchParamsValueType)

Sets a specific search parameter.

usp.set('q', 'new-value');

usp.append(params: Partial<T>)

Appends multiple search parameters without replacing existing ones.

usp.append({ instock: true });

usp.assign(params: Partial<T>)

Sets multiple search parameters, replacing existing ones.

usp.assign({ q: 'updated', page: 5 });

usp.remove(key: keyof T)

Deletes a specific search parameter.

usp.remove('page');

usp.removeAll()

Removes all search parameters.

usp.removeAll();

License

MIT