A React hook for efficiently accessing access local storage.
# yarn
yarn add @weekrise/use-localstorage
# npm
npm install --save @weekrise/use-localstorage
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>
)
}
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 ...
}
MIT © Tobias Herber