react-storage-hook
TypeScript icon, indicating that this package has built-in type declarations

2.0.4 • Public • Published

react-storage-hook

Javascript library for synchronously managing localStorage / sessionStorage. Typescript types included.

demo.gif

Installation

yarn add react-storage-hook

Usage

See also live example with sourcemaps and the local copy.

import { useStorage } from 'react-storage-hook';
 
const SavedCheckbox = () => {
  const [checked, setChecked] = useStorage('saved-checkbox-checked', {
    placeholder: false,
    // storageArea: sessionStorage // to use session storage instead
  })
 
  const onChange = e => setChecked(e.target.checked);
 
  return <input {...{
    checked,
    onChange,
    type: "checkbox"
  }}>
}

The checked state will be stored and synchronized between instances of the page.

The typescript types are authoratative for API scheme.

Package Sidebar

Install

npm i react-storage-hook

Weekly Downloads

33

Version

2.0.4

License

MIT

Unpacked Size

8.63 kB

Total Files

12

Last publish

Collaborators

  • zemnmez