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

@dipakshiroya/react-use-url-state

v0.1.2

Published

A smart React hook to sync deeply nested state with the URL query or hash with schema validation, versioning, and debounced updates.

Readme

@dipakshiroya/react-use-url-state

A smart React hook to sync deeply nested state with the URL query or hash, with debouncing, versioning, and schema validation.


Features

  • Syncs React state to URL query parameters or hash.
  • Handles deeply nested objects.
  • Two-way sync: URL → state on load / popstate, and state → URL on change.
  • Debounced URL updates.
  • Versioning to detect incompatible state shapes.
  • Optional schema validation / coercion.
  • Supports two modes:
    • single (encodes full state in one base64 param)
    • flat (flattened keys in query string)

Installation

npm install @dipakshiroya/react-use-url-state

or

yarn add @dipakshiroya/react-use-url-state

Quick Example

This example syncs a single query parameter q in the URL with a text input:

import React from "react";
import { useUrlState } from "@dipakshiroya/react-use-url-state";

export default function App() {
  // Initial state { q: "" } will load from ?q=... if present
  const [state, setState] = useUrlState<{ q: string }>({ q: "" });

  return (
    <div style={{ padding: 20 }}>
      <h1>🔍 Search Demo</h1>
      <input
        type="text"
        placeholder="Type to update ?q=..."
        value={state.q}
        onChange={(e) => setState({ q: e.target.value })}
        style={{ padding: "8px", fontSize: "16px" }}
      />
      <p>Current query: <b>{state.q}</b></p>
      <p>Try typing something and see the URL update automatically!</p>
    </div>
  );
}

Advanced Example: Nested Filters

import React from "react";
import { useUrlState } from "@dipakshiroya/react-use-url-state";

const defaultFilters = {
  q: "",
  page: 1,
  perPage: 20,
  filters: {
    categories: [] as string[],
    price: { min: 0, max: 200 },
    showOutOfStock: false,
  },
};

export default function FilterApp() {
  const [filters, setFilters, flush] = useUrlState(defaultFilters, {
    param: "state",
    debounceMs: 300,
    version: "v1",
    mode: "single",
    validator: (incoming) => {
      if (!incoming || typeof incoming !== "object") return { ok: false };
      const clean = { ...defaultFilters, ...(incoming as any) };
      clean.page = Math.max(1, Number(clean.page) || 1);
      clean.perPage = Math.max(1, Math.min(100, Number(clean.perPage) || 20));
      if (clean.filters?.price) {
        clean.filters.price.min = Number(clean.filters.price.min) || 0;
        clean.filters.price.max = Number(clean.filters.price.max) || 0;
      }
      return { ok: true, value: clean };
    },
  });

  return (
    <div style={{ padding: 20 }}>
      <h2>Advanced Filters Demo</h2>
      <input
        placeholder="Search"
        value={filters.q}
        onChange={(e) => setFilters({ ...filters, q: e.target.value })}
      />
      <input
        type="number"
        value={filters.page}
        onChange={(e) => setFilters({ ...filters, page: Number(e.target.value) })}
      />
      <button onClick={() => flush()}>Flush to URL now</button>
      <pre>{JSON.stringify(filters, null, 2)}</pre>
    </div>
  );
}

How it works

  • Typing in the input automatically updates the URL query string.
  • Reloading the page preserves the state from the URL.
  • Supports nested objects with optional schema validation.
  • Debounced updates prevent excessive URL changes.

License

MIT © Dipak Shiroya