Skip to content

weekrise/use-localstorage

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

13 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Use Localstorage

A React hook for efficiently accessing access local storage.

Install

# yarn
yarn add @weekrise/use-localstorage

# npm
npm install --save @weekrise/use-localstorage

Usage

The data is serialized to JSON which means that you can store numbers, strings, objects, arrays or anything else that can be passed to JSON.stringify.

import createLocalstorageState from '@weekrise/use-localstorage';

let useLocalstorage = createLocalstorageState('key-name', 3);

let App = () => {
  let [count, setCount] = useLocalstorage();

  return (
    <div>
      <h1>{count}</h1>
      <button onClick={() => setCount(count + 1)}>+1</button>
    </div>
  )
}

Usage with Typescript

import createLocalstorageState from '@weekrise/use-localstorage';

let useLocalstorage = createLocalstorageState<{
  name: string;
  email: string;
}>('key-name', { name: '...', email: '...' });

let App = () => {
  let [data, setData] = useLocalstorage();

  // use data ...
}

License

MIT © Tobias Herber