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

@cullenbond/use-local-storage-sync

v1.0.7

Published

React hook to save state to local storage and sync when updates are made to local storage

Downloads

20

Readme

useLocalStorageSync Hook (React)

To get started, simply run the following cmd to install the package:

npm install @cullenbond/use-local-storage-sync

Then import into the hook into your react component.

import { useLocalStorageSync } from "@cullenbond/use-local-storage-sync";

Example 1:

const [showButtons, setShowButtons] = useLocalStorageSync("showButtons", false);

Example 2:

const [persistedCount, setPersistedCount] = useLocalStorageSync("persistedCount", 0);

Example 3

const [userId, setUserId] = useLocalStorageSync("userId", "bob");

Example 4

const [groceryList, setGroceryList] = useLocalStorageSync("groceryList", ["Apples", "Oranges", "Bananas"]);

Advanced Loading and State sync usage

An abstract example of load logic using initialization value, to avoid infinite load loops. If you persist and load data to data stores bi-directionally.

const [showMenu, setShowMenu, showMenuError, showMenuInitialized] = useLocalStorageSync("showSettingsMenu", true)

useEffect(()=>{
    if(showMenuInitialized){
        // do things that might otherwise cause side effects
        // update redux store to localStorage value, after initialization
    }
},[showMenu])

useEffect(()=>{
    if(showMenuInitialized){
        // do things that might otherwise cause side effects
        // update localStorage value, after initialization and when state is updated
        setShowMenu(state.showMenu)
    }
},[state.showMenu])

Parameters:

  1. Value Key Name

Similar to how JS Map() works we will identify the name of our value that is to be stored.

It is important to note that if you might to be persisting several variations of a name, to make sure the key name is unique.

Example:

const [buttonClickCount, setButtonClickCount] = useLocalStorageSync("buttonClickCount")
  1. Default Initial Value

The initial value takes precedence if no existing values have been stored locally.

Example:

const [buttonClickCount, setButtonClickCount] = useLocalStorageSync("buttonClickCount", 0)
  1. (Optional) getItem function

Method called when retrieving a stored value, before setting value to be used in state.

The hook will json.parse() the stored value, by default

Example:

const [buttonClickCount, setButtonClickCount] = useLocalStorageSync("buttonClickCount", 0, value => JSON.parse(value))
  1. (Optional) setItem function

Method called when setting a stored or synced value before setting the value.

The hook will json.stringify() the stored value, by default

Example:

const [buttonClickCount, setButtonClickCount] = useLocalStorageSync("buttonClickCount", 0, value => JSON.parse(value), value => JSON.stringify(value))

Avoid errors

  • Overusing the hook with the same key can cause performance issues.
  • Do not use with the same key in re-used components.
    • unless you intentionally want to reference and update the same value

Common Usecases

  • Storing website settings.
  • Initializing Redux store on load.
  • Persisting data for offline scenarios.
  • Updating state across tabs and windows, in same browser

Anti-patterns:

  • Avoid using in a compononent that is re-used often, and key points to the same value.
  • Do not persist data that doesn't need to be stored long-term.

Return values

  1. value
  2. setValue
  3. error
  4. initialized