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 local storage state done right

  • 1.0.3
  • Source
  • npm
  • Socket score

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

use-local-storage-state

React hook with the same API as useState() but persisting the data in local storage

Install

$ npm install use-local-storage-state

Why?

Why this module even exists? There are more than a few libraries to achieve almost the same thing. I created this module in frustration with the big number of non-optimal solutions. Below are the things that this module does right. All bullets are things that some other library isn't doing. Put all together there isn't a single library that solves all these problems:

  • 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 which is based on Caret - Markdown Editor for Mac / Windows and is in private beta.
  • Supports creating a global hook that can be used in multiple places. See the last example in the Usage section.

Usage

import useLocalStorageState from 'use-local-storage-state'

const [todos, setTodos] = useLocalStorageState('todos', [
    'buy milk',
    'do 50 push-ups'
])

Complete todo list example:

import React, { useState } from 'react'
import useLocalStorageState from 'use-local-storage-state'

export default function Todos() {
    const [query, setQuery] = useState('')
    const [todos, setTodos] = useLocalStorageState('todos', ['buy milk'])

    function onClick() {
        setTodos([...todos, query])
    }

    return (
        <>
            <input value={query} onChange={e => setQuery(e.target.value)} />
            <button onClick={onClick}>Create</button>
            {todos.map(todo => (<div>{todo}</div>))}
        </>
    )
}

Using the same data from the storage in multiple places:

import { createLocalStorageStateHook } from 'use-local-storage-state'

// store.ts
export const useTodos = createLocalStorageStateHook('todos', [
    'buy milk',
    'do 50 push-ups'
])

// Todos.ts
function Todos() {
    const [todos, setTodos] = useTodos()
}

// Popup.ts
function Popup() {
    const [todos, setTodos] = useTodos()
}

API

useLocalStorageState(key, defaultValue?)

Returns the same value that React useState() returns.
Look at Usage section for an example.

key

Type: string

The key that will be used when calling localStorage.setItem(key)and localStorage.getItem(key).
⚠️ Be careful with name conflicts as it is possible to access a property which is already in localStorage that was created from another place in the codebase or in an old version of the application.

defaultValue

Type: any Default: undefined

The initial value of the data. The same as useState(defaultValue) property.

createLocalStorageStateHook(key, defaultValue?)

Returns a hook to be used in multiple places.
Look at Usage section for an example.

key

Type: string

The key that will be used when calling localStorage.setItem(key)and localStorage.getItem(key).
⚠️ Be careful with name conflicts as it is possible to access a property which is already in localStorage that was created from another place in the codebase or in an old version of the application.

defaultValue

Type: any Default: undefined

The initial value of the data. The same as useState(defaultValue) property.

Keywords

FAQs

Package last updated on 15 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