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

@jokullsolberg/next-use-search-params

v0.2.0

Published

React hook for type safe search param use in Next.js.

Downloads

13

Readme

useSearchParams()

Type-safe search param handling for Next.js using zod.

Install

npm install @jokullsolberg/next-use-search-params

Example usage

function Signup() {
  const [{ foo, bar, date }, setSearchParam] = useSearchParams({
    foo: z.string().default(''),
    bar: z.coerce.number().default(1),
    date: z.coerce.date().default(new Date()),
    screen: z.enum(['login', 'signup']).default('login'),
  });
  return (
    <div
      style={{
        display: 'flex',
        flexDirection: 'column',
        gap: '10px',
        alignItems: 'start',
      }}
    >
      <h2>Signup</h2>
      <input
        value={foo}
        onChange={(event) => setSearchParam('foo', event.target.value)}
      />
      <input
        type="number"
        value={String(bar)}
        onChange={(event) => setSearchParam('bar', event.target.valueAsNumber)}
      />
      <input
        type="date"
        value={date.toISOString().split('T')[0]}
        onChange={(event) =>
          setSearchParam('date', event.target.valueAsDate ?? new Date())
        }
      />
      <p>{date.toISOString()}</p>
      <button onClick={() => setSearchParam('screen', 'login')}>Login</button>
    </div>
  );
}

export default function Page() {
  const [{ screen }, setSearchParam] = useSearchParams({
    screen: z.enum(['login', 'signup']).default('login'),
  });
  return screen === 'login' ? (
    <div>
      Login{' '}
      <button onClick={() => setSearchParam('screen', 'signup')}>Signup</button>
    </div>
  ) : (
    <Signup />
  );
}

These components can affect the state by simply routing with Link or router.push — instead of prop drilling onSuccess handlers into each component to orchestrate transitions.

Default values are required. Let me know if this is annoying.

Benefits

Keeping state in search params is user friendly and has DX benefits

  • State lives in search params, like a poor man's global Context.
  • Browser back button works and updates state.
  • zod is a powerful tool to validate data and type values.
  • Default values are not displayed in the URL to keep it clean.
  • Because state is global multiple components can bind simultaneously to the same param. This helps avoid prop drilling onSuccess handlers.

FAQ

  • URLSearchParams support multiple values for the same key, does this tool too? No.
  • Values of URLSearchParams are always strings, do I need to zod preprocess them? No. Use the new z.coerce stuff instead. Internally we call JSON.stringify or Date.toISOString() to marshal back to URL.
  • What happens to unexpected or illegal values? They are ignored and default values are used instead.

Next.js 13 and SSR

This does not work inside /app client components since router is imported from next/navigation now.

SSR will reach for the default values.