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-router-dom-search-params

v1.1.2

Published

A utility that integrates search params into React components

Readme

react-router-dom-search-params

A nifty library to help you manage link props in your components to params in the URL (?key1=value1&key2=true). You can define which params should be automatically kept between pages (such as ?lang=en) and map your search params to props and setter props on your components.

Installation

Using npm:

$ npm install --save react-router-dom-search-params

Usage

This library works with react-router-dom and allows you to map search params in the URL of your document to props of your React component.

Setup

You must enclose the components using this module in a <ParamProvider> component. This component optionally accepts a keep prop, that defines the name of the params that should be kept when navigating from one page to another.

import { ParamProvider } from 'react-router-dom-search-params';

function App() {
  return (
    <ParamProvider keep={['lang']}>
      /* your app */
    </ParamProvider>
  );
}

Wrap Components To Provide Search Params Props

The easiest way to use this module is to wrap your components with withParams(). This will expose a pair of props to the wrapped component, one with the value of the param, and another that will be a setter function to update the value (and change the current URL).

import { withParams } from 'react-router-dom-search-params';

/* the component accepts a `checked` prop that will be set to the current value of the param,
   and a `setChecked` callback that will set the value of the param */
function Demo({ checked, setChecked }) {
  return <input type="checkbox" checked={checked} onChange={e => setChecked(e.target.checked)} />;
}

const DemoParam = withParams(Demo, {
  checked: { defaultValue: false },
});

If you run this demo, the URL will switch to ?checked=true anytime the checkbox is checked.

Hooks

Alternatively, you can also use hooks to get and set the param values.

import { useSearchParams } from 'react-router-dom-search-params';

function Demo() {
  const searchParams = useSearchParams();
  /* note that this works like `useState()` */
  const [ checked, setChecked ] = searchParams.param('checked', false);
  return <input type="checkbox" checked={checked} onChange={e => setChecked(e.target.checked)} />;
}

Automatic casts

As a convenience, when the defaultValue is a boolean, then the value you receive will be cast to a boolean (0, off and false are false, anything else is true).

When the defaultValue is an array, then the value is split by commas.

If the defaultValue is an object, then all the params whose name is prefixed with name will be merged in an array.

If you need you own serialization/deserialization, provide a defaultValue that is a string and implement the logic yourself.

Generating your own URLs

You can generate an URL with all the params that should be kept between pages and a new URL using the useURL() hook, that returns a function that you can call with another URL:

const url = useURL();
console.log(url('/another-page'));

Linking to another page while keeping some params

Instead of using Link from the react-router-dom package, you can use ParamLink that react-router-dom-search-params provides.

This will allow you to place links on your component, that will merge the params you provide, and the params that should be kept (as defined in ParamProvider):

return <ParamLink to="/browse" params={{shop: 'apples'}}>Browse Apples</ParamLink>;

Quality

This package is tested with jest and has full 100% coverage. It's a small package so it shouldn't break.

If you want to submit a PR, please make sure that it still passes ESLint (npm run lint) and that all tests passes (npm run test).

Issues

Please report an issue on the project if you have any questions or comments.