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-use-local-storage-state

v1.1.0

Published

Like useState, but it also saves in localStorage

Readme

react-use-local-storage-state

A simple React hook to persist state to localStorage.
Easily keep your app's state in sync with the browser's storage, so your users' data survives page reloads.


Features

  • Drop-in replacement for useState: Same API, just add a key!
  • Automatic persistence: State is saved to and loaded from localStorage.
  • TypeScript support: Fully typed for a great developer experience.
  • SSR-safe: Handles server-side rendering gracefully.

Installation

npm install react-use-local-storage-state

or

yarn add react-use-local-storage-state

Usage

import useLocalStorageState from "react-use-local-storage-state";

function Counter() {
  const [count, setCount] = useLocalStorageState("my-counter", 0);

  return (
    <div>
      <p>Count: {count}</p>
      <button onClick={() => setCount((c) => c + 1)}>Increment</button>
    </div>
  );
}

With complex values

You can store objects, arrays, or any JSON-serializable value:

const [user, setUser] = useLocalStorageState("user", { name: "Alice" });

With lazy initialization

You can pass a function as the initial value (just like useState):

const [value, setValue] = useLocalStorageState("expensive", () => computeInitial());

API

const [state, setState] = useLocalStorageState<T>(
  key: string,
  initialValue: T | (() => T)
);
  • key: The localStorage key to use.
  • initialValue: The initial value, or a function returning the initial value.
  • Returns: [state, setState] — just like useState.

Advanced: Creating Custom Hooks with createLocalStorageStateHook

If you want to reuse the same localStorage key and initial value across multiple components, you can create a custom hook using createLocalStorageStateHook.

import useLocalStorageState from "react-use-local-storage-state";

/**
 * Create a custom hook bound to a specific localStorage key and initial value.
 */
function createLocalStorageStateHook<T>(
  key: string,
  initialValue: T | (() => T)
) {
  return function useCustomLocalStorageState(): [
    T,
    React.Dispatch<React.SetStateAction<T>>
  ] {
    return useLocalStorageState<T>(key, initialValue);
  };
}

Example: Shared Theme State

Suppose you want to share a theme preference ("light" or "dark") across your app:

// Create a custom hook for the theme
const useThemeState = createLocalStorageStateHook("theme", "light");

function ThemeSwitcher() {
  const [theme, setTheme] = useThemeState();

  return (
    <div>
      <p>Current theme: {theme}</p>
      <button onClick={() => setTheme(theme === "light" ? "dark" : "light")}>
        Toggle Theme
      </button>
    </div>
  );
}

// You can use useThemeState in any component to access or update the theme
function ThemeStatus() {
  const [theme] = useThemeState();
  return <span>Theme is {theme}</span>;
}

Now, all components using useThemeState will read and write to the same localStorage key, keeping the theme in sync across your app.


How it works

  • On mount, the hook checks localStorage for the given key.
    • If found, it parses and uses that value.
    • If not, it uses the provided initialValue.
  • Whenever the state changes, it is saved to localStorage.
  • Handles errors gracefully and works in SSR environments.

Caveats

  • No cross-tab sync: Changes in one tab are not automatically reflected in others.
  • JSON serialization: Only JSON-serializable values are supported.
  • Storage limits: localStorage has size limits (usually ~5MB).

License

MIT


Credits

Inspired by the React community and the need for simple persistent state.