Latest Threat ResearchGlassWorm Loader Hits Open VSX via Developer Account Compromise.Details
Socket
Book a DemoInstallSign in
Socket

storagehooks

Package Overview
Dependencies
Maintainers
1
Versions
4
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

storagehooks

React hooks for accessing state in `localStorage` and `sessionStorage`, with support for encoding values as JSON and tracking `storage` events

Source
npmnpm
Version
1.0.0
Version published
Weekly downloads
2
-33.33%
Maintainers
1
Weekly downloads
 
Created
Source

storagehooks

React hooks for connecting component state to state in localStorage and sessionStorage.

Features:

  • Supports localStorage and sessionStorage
  • Values may be any JSON data type
  • Listens to storage events to detect changes to storage state
  • Simple API (similar to React's useState())

storagehooks

Usage

Import useLocalStorage or useSessionStorage (based on your preferred Web Storage location)

import {useLocalStorage, useSessionStorage} from 'storagehooks';

function myComponent(props) {
  // Use it like `useState()`, but provide name of storage key as the first argument
  const [localValue, setLocalValue] = useLocalStorage('myKey', 'initial value');
  const [sessionValue, setSessionValue] = useSessionStorage('myKey', 'initial value');

  return <>
    <label>Value in localStorage:</label>
    <input value={localValue} onChange={e => setLocalValue(e.target.value)} />

    <label>Value in sessionStorage:</label>
    <input value={sessionValue} onChange={e => setSessionValue(e.target.value)} />
  </>;
}

Keywords

hook

FAQs

Package last updated on 24 May 2021

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