New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

use-local-storage-state

Package Overview
Dependencies
Maintainers
1
Versions
83
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

use-local-storage-state

React hook for using local storage the same way you use setState()

  • 0.2.1
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
69K
decreased by-22.04%
Maintainers
1
Weekly downloads
 
Created
Source

use-local-storage-state

Like useState() but for local storage

Install

$ npm install use-local-storage-state

Why?

Why this module should even exists. There are more than a few libraries to achieve almost the same thing. I created in frustration in the big number of non-optimal current solutions. Here are the things that this module does right. All things mentioned are things that some other library doesn't do. Put all together there isn't a library that solves all cases:

  • Written in TypeScript. useLocalStorageState() returns absolutely the same type as React useState().
  • Uses JSON.parse() and JSON.stringify() to support non string values.
  • Subscribes to the Window storage event which tracks changes across browser tabs and iframe's.
  • Used in a production application. The product is based on Caret - Markdown Editor for Mac / Windows and is in private beta.

Usage

const [todos, setTodos] = useLocalStorageState('todos', [
    'buy milk',
    'do 50 push-ups'
])
import useLocalStorageState from 'use-local-storage-state'

function Todos() {
    const [query, setQuery] = useState('')
    const [todos, setTodos] = useLocalStorageState('todos', [])
    
    function onClick() {
        setTodos([...todos, query])
    }
    
    return (
        <>
            <input value={query} onChange={e => setQuery(e.value}} />
            {todos.map(todo => <div>{todo}</div>)}
        </>
    )
}

Keywords

FAQs

Package last updated on 12 Mar 2020

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc