1.0.3 • Published 4 years ago

react-hook-uselocalstorage v1.0.3

Weekly downloads
2
License
MIT
Repository
github
Last release
4 years ago

useLocalStorage - React custom hook

React custom Hook to manage localStorage API.

Why useLocalStorage?

With this hook, you can handle your localStorage:

TL;DR;

const storageVariable, setStorageVariable = useLocalStorage('storage data');

Usage

Just install:

npm install react-hook-uselocalstorage

And import the hook:

import useLocalStorage from 'react-hook-uselocalstorage';

Use it in your component:

import React, { useRef } from 'react'
import useLocalStorage from 'react-hook-uselocalstorage'

const App = () => {
  const [storageVariable, setStorageVariable] = useLocalStorage('storage data');
  const inputRef = useRef(null);

  const clickHandler = () => {
    setStorageVariable(inputRef.current.value);
  }

  return (
    <div>
      <h2>{storageVariable}</h2>

      <input ref={inputRef} type='text' />
      <button onClick={clickHandler}>Set</button>
    </div>
  )
}

export default App

LIVE Example

(Check it in deployed example)

License

MIT © franlol