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

@souvikelric/uselocalstorage-hook

v0.0.4

Published

A React hook for syncing localstorage with state

Readme

📦 use-local-storage-sync

A lightweight React hook and helper utility for syncing localStorage or sessionStorage with React state. Useful for persisting state across sessions and sharing data across different parts of your app (including Context, Redux, or external state managers).

✨ Features

  • 🔄 Sync React state with localStorage or sessionStorage
  • ⚡ Automatically update storage on state change
  • 🧹 Utilities to reset a specific key or clear all storage
  • 🚀 Ready for use with Context, Redux, or other state tools
  • ✅ SSR-safe utility helper for read/write access

📦 Installation

npm install use-local-storage-sync
# or
yarn add use-local-storage-sync

🔧 Usage

useLocalStorage Hook

import { useLocalStorage } from "use-local-storage-sync";

function Counter() {
  const { value, setValue, resetItem, clearAllItems } = useLocalStorage({
    key: "counter",
    initialValue: 0,
    storageType: "local", // or 'session'
  });

  return (
    <div>
      <h2>Counter: {value}</h2>
      <button onClick={() => setValue(value + 1)}>Increment</button>
      <button onClick={() => resetItem("counter")}>Reset</button>
      <button onClick={clearAllItems}>Clear All</button>
    </div>
  );
}

🔘 Selective Syncing

Only persist specific fields from a larger object using the include option.

const { value, setValue } = useLocalStorage({
  key: "user",
  initialValue: { name: "", email: "", token: "" },
  include: ["name", "email"], // token is not stored in localStorage
});
This helps avoid storing sensitive data (like tokens) or reduce storage size

✅ Parameters

Name Type Default Description key string — Storage key initialValue T — Initial value if nothing is stored storageType "local" or "session" "local" Which storage to use

🔁 Returns

`const {
  value,           // The current stateful value
  setValue,        // Function to update the value
  resetItem,       // Removes the item from storage and resets state
  clearAllItems,   // Clears the entire storage and resets all keys using this hook
} = useLocalStorage(...)`;

localStorageHelper Utility Use this when you're outside React (e.g., Redux, Context, plain JS modules).

import { localStorageHelper } from "use-local-storage-sync";

// Read a value from localStorage
const user = localStorageHelper.get("user", { name: "Guest" });

// Write a value to localStorage
localStorageHelper.set("user", { name: "John Doe" });

🧪 Example with Context API

const UserContext = createContext(null);

export const UserProvider = ({ children }) => {
  const { value: user, setValue: setUser } = useLocalStorage({
    key: "user",
    initialValue: null,
  });

  return (
    <UserContext.Provider value={{ user, setUser }}>
      {children}
    </UserContext.Provider>
  );
};

📄 License

MIT

🙌 Contributing

Found a bug or have a suggestion? Feel free to open an issue or submit a PR!


Let me know if you want to include badges, a changelog section, or an example project link.