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

uselocalstorage-ts

v1.0.2

Published

React hook to work with localstorage, written in Typescript.

Readme

useLocalStorage

npm downloads bundle size

A React hook for managing local storage with built-in synchronization across tabs.

Installation

You can install the uselocalstorage-ts package via npm (or whatever):

npm install uselocalstorage-ts

Hook Description

The useLocalStorage stores values in localStorage, which allows data to persist across sessions. The hook also listens for changes in localStorage and updates the component when the relevant data changes.

Function Signature

useLocalStorage<Value, InitialValue>(key: string, initValue: InitialValue): Value | InitialValue
  • key: string: The key under which the data will be stored in localStorage.
  • initValue: InitialValue: The initial value. It can either be a value or a function that returns the initial value.

The hook returns the value stored in localStorage or the initial value if no data is found.

Hook Behavior

  1. Initialization: On the first render, the hook attempts to retrieve data from localStorage using the provided key.
  2. Subscription to Changes: The hook subscribes to the storage event, which is triggered when data in localStorage changes. When the data for the same key changes, the component will re-render.
  3. Changing Values: The hook uses a helper function setValueForLocalStorage to store new values in localStorage and sync them across different windows or tabs.

Helper Functions

In addition to the main hook, the library provides several helper functions for working with localStorage:

setValueForLocalStorage

export const setValueForLocalStorage<NewValue>(key: string, newValue: NewValue): void

This function sets a value in localStorage for the specified key. If the value is a function, it is applied to the current value in localStorage (if available), and the result is saved.

  • Parameters:
    • key: string: The key to store the value under.
    • newValue: NewValue: The new value to store, or a function that will be applied to the current value.
  • Returns: Does not return anything.

removeKeyFromLocalStorage

export const removeKeyFromLocalStorage(key: string): void

Removes an item from localStorage by key and triggers the storage event to synchronize with other windows/tabs.

  • Parameters:
    • key: string: The key for the item to remove.
  • Returns: Does not return anything.

removeKeysFromLocalStorage

export const removeKeysFromLocalStorage(keys: string[]): void

Removes multiple keys from localStorage and triggers the storage event for each change.

  • Parameters:
    • keys: string[]: An array of keys to remove.
    • Returns: Does not return anything.

clearLocalStorage

export const clearLocalStorage(): void

Removes all data from localStorage and triggers the storage event for each change.

  • Parameters: None.
  • Returns: Does not return anything.

Usage

The useLocalStorage hook allows you to easily read and write values from local storage. It also automatically synchronizes changes across different components of your application. Function setValueForLocalStorage allows you to change value wherever you want.

Example

import { useLocalStorage, setValueForLocalStorage } from 'uselocalstorage-ts';

const MyComponent = () => {
	const inputValue = useLocalStorage('myKey', 'defaultValue');

	return (
		<div>
			<input
				type='text'
				value={inputValue}
				onChange={(e) => setValueForLocalStorage(e.target.value)}
			/>
			<p>Current Value: {value}</p>
		</div>
	);
};

// AnotherComponent.tsx
import { setValueForLocalStorage } from 'uselocalstorage-ts';

const AnotherMyComponent = () => {
	return (
		<button onClick={() => setValueForLocalStorage('myKey', 'AnotherValue')}>
			Change localStorage!{' '}
		</button>
	);
};