@blocdigital/uselocalstorage
TypeScript icon, indicating that this package has built-in type declarations

0.1.4 • Public • Published

uselocalstorage

Handle interaction with local and session storage

Install

npm install --save @blocdigital/uselocalstorage

Usage

import {useState, useEffect} from 'react';

import useLocalStorage from '@blocdigital/uselocalstorage';

const Example = () => {
  const [state, setState] = useState('hello world');

  // initiate the session storage
  const storage = useLocalStorage('session');

  // initialise the storage state
  useEffect(() => {
    storage.init('state', 'hello world');
  }, [storage]);

  // set up listeners to keep state in sync with storage
  useEffect(() => {
    const onStateChange = (key) => {
      if(key !== 'state') return;

      setState(storage.get(key));
    }

    storage.on('set', onStateChange);

    // remember to tidy up you event listeners
    return () => storage.off('set', onStateChange);
  }, [storage]);


  return (
    <div>
      <span>Current state: {state}</span>
      <br />
      <button onClick={()=>{
        storage.set('state', String(Date.now()));
      }}>
        Change State
      </button>
    </div>
  );
};

Package Sidebar

Install

npm i @blocdigital/uselocalstorage

Weekly Downloads

7

Version

0.1.4

License

MIT

Unpacked Size

18.5 kB

Total Files

9

Last publish

Collaborators

  • hwickham
  • nadakbar
  • acol
  • link2twenty