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 πŸ™

Β© 2024 – Pkg Stats / Ryan Hefner

react-storage-complete

v1.1.12

Published

πŸ—„οΈ React hooks for accessing localStorage and sessionStorage, with syncing and prefix support. The complete package.

Downloads

1,290

Readme

Documentation

Read the official documentation.

πŸ‘οΈ Live Demo

Overview

Hooks for easily accessing localStorage and sessionStorage, with a similar interface to React.useState().

Features include:

  • πŸ”„ Automatic state synchronization
    • Changes are synchronized across hooks, and even different browser tabs, automatically.
  • πŸ‘¨β€πŸ‘©β€πŸ‘§β€πŸ‘¦ Namespacing using prefixes
    • Easily scope your stored data with namespaces. Great for managing data for multiple users.
  • πŸ”’ Support for primitives and objects
    • Store and retrieve strings, booleans, numbers, and objects effortlessly.
  • πŸ‘Ύ Customizable
    • Want to store something unusual? Just provide your own encoder and decoder.
  • πŸ’ Default values
    • Optional support for defaults is baked right in.

Donate

If this project helped you, please consider buying me a coffee or sponsoring me. Your support is much appreciated!

Β 

Table of Contents

Installation

npm i react-storage-complete

Quick Start

| Use this hook... | For this Storage API... | Storage Description | | -------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------ | -------------------------------------------------------------------------------------------------------------------------------------------------------- | | useLocalStorage | localStorage | The localStorage object stores data with no expiration date. The data is not deleted when the browser is closed, and is available for future sessions. | | useSessionStorage | sessionStorage | The sessionStorage object stores data for only one session. The data is deleted when the browser is closed. |

Basic Usage

import { useLocalStorage } from 'react-storage-complete';
import { useSessionStorage } from 'react-storage-complete';

Both useLocalStorage and useSessionStorage share the same interface and are interchangeable.

In your component or hook:

const [name, setName] = useLocalStorage('name');

This will store and retrieve the value using the name key in localStorage.

Note: If using TypeScript, you can provide a value type like so: useLocalStorage<string>('name').

Providing A Default Value

You can also provide a default value for when the stored value is undefined (not currently set in localStorage):

const [name, setName] = useLocalStorage('name', 'Guest');

In this example, when the stored item with the key name is undefined, the name value will be Guest.

Note: When a value is null in storage, null is returned, not the provided default.

Namespacing Using Prefix

You can namespace stored items using a prefix:

const [name, setName] = useLocalStorage('name', 'Guest', {
  prefix: 'my-namespace',
});

For example, by using a user ID as the prefix, you can scope the stored data to the user and avoid mixing settings.

Above, the key for the value would be my-namespace.name.

Namespacing Using User IDs

With react-storage-complete, managing stored data for multiple user accounts in the same browser is easy.

const [name, setName, initialized] = useLocalStorage('name', 'Guest', {
  prefix: user?.id, // Undefined until user ID is available as a namespace
  shouldInitialize: !!user?.id, // Only initialize when user ID is available
});

As shown above, you can delay initialization of the stored value until your prefix is available and ready to use as the namespace.

For example, a user ID may not be ready until logged in, but you may still be calling the hook in your app before that happens.

In this example:

  • initialized will be false until the user.id is loaded and ready to use as the prefix.
  • When shouldInitialize is true, the prefix is used with the key to retrieve the stored value from localStorage.
  • Then, initialized will return as true, and your value will be ready to use!

Clearing The Value

const [name, setName, initialized, clear] = useLocalStorage('name', 'Guest');

You can clear any stored value and completely remove it from storage using the returned clear function.

In this example, calling clear() will delete the item from storage. You can also call setName(undefined) to achieve the same result.

Accessing The Prefixed Key

const [food, setFood, initialized, clear, prefixedKey] = useLocalStorage('food', 'Hamburger', {
  prefix: 'my-namespace',
});

The hook also returns the prefixed key, in case you want direct access to it.

In the example above, the prefixed key would be my-namespace.food.

You can customize the separator by providing the prefixSeparator option.

Additional Options & Uses

See the documentation for additional options and uses:

TypeScript

Type definitions have been included for TypeScript support.

Icon Attribution

Favicon by Twemoji.

Contributing

Open source software is awesome and so are you. 😎

Feel free to submit a pull request for bugs or additions, and make sure to update tests as appropriate. If you find a mistake in the docs, send a PR! Even the smallest changes help.

For major changes, open an issue first to discuss what you'd like to change.

⭐ Found It Helpful? Star It!

If you found this project helpful, let the community know by giving it a star: πŸ‘‰β­

License

See LICENSE.md.