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 🙏

© 2024 – Pkg Stats / Ryan Hefner

use-push-router

v0.0.5

Published

A custom hook that simplifies modifying the search params in Next.js

Downloads

17

Readme

use-push-router

A custom hook that simplifies modifying the search params in Next.js and navigates to the new URL.

Installation

npm i use-push-router

Usage

import { usePushRoute } from 'use-push-router';

const { pushSearchParams } = usePushRoute();

The pushSearchParams function takes an object with the following shape:

{
  add?: Record<string, string | string[]>;
  remove?: Record<string, string | string[] | undefined>;
  set?: Record<string, string | string[]>;
}

Adding search params

import { usePushRoute } from 'use-push-router';

const Component = () => {
  const { pushSearchParams } = usePushRoute();

  const handleClick = () => {
    pushSearchParams({
      add: {
        foo: 'bar', // adds foo=bar to the search params. If there is already a value for foo, it will be an array of values.
        baz: ['qux', 'quux'], // adds baz=qux&baz=quux to the search params.
      },
    });
  };

  return <button onClick={handleClick}>Add search params</button>;
};

Therea are two ways to add parameters to the URL:

  1. Specify a key-value pair to add a specific parameter value: foo: 'bar'. After calling this function, foo=bar will be added to the URL. If there is already a value for foo, for example https://example.com/?foo=bar, it will become https://example.com/?foo=bar&foo=qux after calling this function.
  2. Use an array to add multiple values for the same parameter: baz: ['qux', 'quux']. After calling this function, baz=qux&baz=quux will be added to the URL.

Setting search params

import { usePushRoute } from 'use-push-router';

const Component = () => {
  const { pushSearchParams } = usePushRoute();

  const handleClick = () => {
    pushSearchParams({
      set: {
        foo: 'bar', // sets foo=bar in the search params. If there is already a value for foo, it will be overwritten.
        baz: ['qux', 'quux'], // sets baz=qux&baz=quux in the search params.
      },
    });
  };

  return <button onClick={handleClick}>Set search params</button>;
};

There are two ways to set parameters in the URL:

  1. Specify a key-value pair to set a specific parameter value: foo: 'bar'. After calling this function, foo=bar will be set in the URL. If there is already a value for foo, for example https://example.com/?foo=qux, it will become https://example.com/?foo=bar after calling this function.
  2. Use an array to set multiple values for the same parameter: baz: ['qux', 'quux']. After calling this function, baz=qux&baz=quux will be set in the URL and replace any existing values for baz.

Removing search params

import { usePushRoute } from 'use-push-router';

const Component = () => {
  const { pushSearchParams } = usePushRoute();

  const handleClick = () => {
    pushSearchParams({
      remove: {
        foo: 'bar', // removes foo=bar from the search params.
        baz: ['qux', 'quux'], // removes baz=qux&baz=quux from the search params.
        qux: undefined, // removes qux from the search params.
      },
    });
  };

  return <button onClick={handleClick}>Remove search params</button>;
};

You can remove parameters in three ways:

  1. Specify a key-value pair to remove a specific parameter value: foo: 'bar'. After calling this function, foo=bar will be removed from the URL if it exists.
  2. Use an array to remove multiple values for the same parameter: baz: ['qux', 'quux']. After calling this function, baz=qux&baz=quux will be removed from the URL if they exist.
  3. Set a parameter to undefined to remove it entirely: qux: undefined. After calling this function, qux will be removed from the URL if it exists.

License

MIT

Author

Nico Prananta. Website: https://nico.fyi. Twitter: https://twitter.com/2co_p