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 🙏

© 2025 – Pkg Stats / Ryan Hefner

use-url-state-reacthook

v0.0.2

Published

A React hook for managing URL state

Readme

🔗 useUrlState

A powerful React hook for managing state synchronized with URL search parameters. Perfect for creating shareable URLs, maintaining filters across page refreshes, and building user-friendly web applications with deep linking support.

TypeScript MIT License React

✨ Features

  • 🎯 Type-safe - Full TypeScript support with generic types
  • 🔄 Automatic URL sync - State changes are reflected in the URL instantly
  • ⏱️ Debouncing - Prevent excessive URL updates during rapid state changes
  • 🧭 Browser history - Full support for back/forward navigation
  • 🏷️ Namespacing - Avoid conflicts when using multiple instances
  • 🛡️ Validation - Built-in sanitization and validation hooks
  • 📦 Custom serialization - Define custom codecs for complex data types
  • 🪶 Lightweight - Zero dependencies (except React peer dependency)

📦 Installation

# Using npm
npm install use-url-state-reacthook

# Using yarn
yarn add use-url-state-reacthook

# Using pnpm
pnpm add use-url-state-reacthook

🚀 Quick Start

import { useUrlState } from "use-url-state-reacthook";

function SearchFilters() {
  const [filters, filtersApi] = useUrlState({
    search: "",
    category: "all",
    page: 1,
  });

  return (
    <div>
      <input
        value={filters.search}
        onChange={(e) => filtersApi.set("search", e.target.value)}
        placeholder="Search..."
      />

      <select
        value={filters.category}
        onChange={(e) => filtersApi.set("category", e.target.value)}
      >
        <option value="all">All Categories</option>
        <option value="tech">Technology</option>
        <option value="design">Design</option>
      </select>

      <div>Current page: {filters.page}</div>
      <button onClick={() => filtersApi.set("page", filters.page + 1)}>
        Next Page
      </button>
    </div>
  );
}

The URL will automatically update to something like: ?search=react&category=tech&page=2

📚 API Reference

useUrlState(defaults?, options?)

Returns a tuple [state, api] where:

  • state: The current state object
  • api: Object with methods to manipulate the state

Parameters

| Parameter | Type | Description | | ---------- | -------------------- | --------------------------------------- | | defaults | T \| (() => T) | Default values for the state (optional) | | options | UrlStateOptions<T> | Configuration options (optional) |

Options

| Option | Type | Default | Description | | ---------------- | ------------------------------------------ | ----------- | -------------------------------------------- | | codecs | Partial<{ [K in keyof T]: Codec<T[K]> }> | {} | Custom serialization for specific properties | | sanitize | (draft: Partial<T>) => Partial<T> | undefined | Validation/sanitization function | | onChange | (state: T, meta) => void | undefined | Callback fired on state changes | | history | 'replace' \| 'push' | 'replace' | Browser history behavior | | debounceMs | number | undefined | Debounce delay for URL updates | | syncOnPopState | boolean | true | Sync state on browser navigation | | namespace | string | undefined | Prefix for URL parameters |

API Methods

| Method | Signature | Description | | ---------- | ---------------------------------------- | ------------------------------ | | setState | (updater: T \| (prev: T) => T) => void | Replace entire state | | get | (key: keyof T) => T[key] | Get value of specific property | | set | (key: keyof T, value: T[key]) => void | Set specific property | | patch | (partial: Partial<T>) => void | Merge partial changes | | remove | (...keys: (keyof T)[]) => void | Remove properties | | clear | () => void | Clear all state |

🎯 Examples

Basic Usage

import { useUrlState } from "use-url-state-reacthook";

function App() {
  const [state, api] = useUrlState({ name: "", age: 0 });

  return (
    <div>
      <input
        value={state.name}
        onChange={(e) => api.set("name", e.target.value)}
      />
      <input
        type="number"
        value={state.age}
        onChange={(e) => api.set("age", parseInt(e.target.value) || 0)}
      />
      <button onClick={() => api.clear()}>Clear All</button>
    </div>
  );
}

With Custom Serialization

interface Filters {
  tags: string[];
  dateRange: { start: Date; end: Date };
  settings: { theme: string; lang: string };
}

const [filters, api] = useUrlState<Filters>(
  {
    tags: [],
    dateRange: { start: new Date(), end: new Date() },
    settings: { theme: "light", lang: "en" },
  },
  {
    codecs: {
      tags: {
        parse: (str) => str.split(",").filter(Boolean),
        format: (tags) => tags.join(","),
      },
      dateRange: {
        parse: (str) => {
          const [start, end] = str.split("|").map((d) => new Date(d));
          return { start, end };
        },
        format: (range) =>
          `${range.start.toISOString()}|${range.end.toISOString()}`,
      },
    },
  }
);

With Validation and Debouncing

const [userPrefs, api] = useUrlState(
  {
    theme: "light",
    fontSize: 16,
    language: "en",
  },
  {
    sanitize: (draft) => ({
      theme: ["light", "dark"].includes(draft.theme) ? draft.theme : "light",
      fontSize: Math.max(12, Math.min(24, draft.fontSize || 16)),
      language: ["en", "fr", "es"].includes(draft.language)
        ? draft.language
        : "en",
    }),
    debounceMs: 300, // Wait 300ms before updating URL
    onChange: (newState, { source }) => {
      console.log(`Preferences updated from ${source}:`, newState);
      // Save to analytics, localStorage, etc.
    },
  }
);

Multiple Hook Instances with Namespacing

function Dashboard() {
  // User filters (prefixed with 'user_')
  const [userFilters, userApi] = useUrlState(
    {
      role: "all",
      department: "all",
    },
    { namespace: "user" }
  );

  // Product filters (prefixed with 'product_')
  const [productFilters, productApi] = useUrlState(
    {
      category: "all",
      inStock: true,
    },
    { namespace: "product" }
  );

  // URL: ?user_role=admin&user_department=engineering&product_category=electronics&product_inStock=true
}

Complex State Management

interface AppState {
  filters: {
    search: string;
    category: string[];
    priceRange: [number, number];
  };
  view: "grid" | "list";
  sort: { field: string; direction: "asc" | "desc" };
}

const [appState, api] = useUrlState<AppState>({
  filters: {
    search: "",
    category: [],
    priceRange: [0, 1000],
  },
  view: "grid",
  sort: { field: "name", direction: "asc" },
});

// Update nested properties
api.patch({
  filters: {
    ...appState.filters,
    search: "new search term",
  },
});

// Toggle sort direction
api.set("sort", {
  ...appState.sort,
  direction: appState.sort.direction === "asc" ? "desc" : "asc",
});

🔧 Advanced Configuration

History Management

// Replace current URL (default)
const [state, api] = useUrlState(defaults, { history: "replace" });

// Create new history entries (enables back/forward navigation between state changes)
const [state, api] = useUrlState(defaults, { history: "push" });

Disabling Browser Navigation Sync

// Don't sync state when user uses back/forward buttons
const [state, api] = useUrlState(defaults, { syncOnPopState: false });

Performance Optimization

// Debounce URL updates for better performance with rapid changes
const [searchState, api] = useUrlState(
  { query: "" },
  {
    debounceMs: 300, // Wait 300ms before updating URL
  }
);

// Perfect for search inputs that update frequently
<input
  value={searchState.query}
  onChange={(e) => api.set("query", e.target.value)}
/>;

📝 TypeScript Support

The hook is fully typed and provides excellent TypeScript integration:

interface UserFilters {
  name: string;
  roles: ("admin" | "user" | "guest")[];
  isActive: boolean;
  metadata?: { lastLogin: Date };
}

// Full type safety
const [filters, api] = useUrlState<UserFilters>({
  name: "",
  roles: [],
  isActive: true,
});

// TypeScript knows the exact shape
api.set("name", "john"); // ✅ Valid
api.set("roles", ["admin", "user"]); // ✅ Valid
api.set("invalidProp", "value"); // ❌ TypeScript error

🤝 Contributing

Contributions are welcome! Please feel free to submit a Pull Request. For major changes, please open an issue first to discuss what you would like to change.

📄 License

This project is licensed under the MIT License - see the LICENSE file for details.

🙏 Acknowledgments

  • Inspired by the need for better URL state management in React applications
  • Built with TypeScript for maximum developer experience
  • Designed to be simple yet powerful for real-world use cases

Happy coding! 🚀 If you find this hook useful, please consider giving it a ⭐ on GitHub!