1.0.5 • Published 5 years ago
@jsxtools/use-local-storage-factory v1.0.5
use-local-storage-factory
use-local-storage-factory returns a hook that provides a state and setter bound to Local Storage.
It is 363 bytes (214 gzipped).
Installation
npm install @jsxtools/use-local-storage-factory
Usage
import { useState } from 'react';
import useLocalStorageFactory from '@jsxtools/use-local-storage-factory';
const useLocalStorage = useEqualStateFactory({ useState });
function Component() {
// the `value` of `counter` will persist after the browser is refreshed
const [value, setValue] = useLocalStorage('counter', 0);
const inc = setValue.bind(null, value + 1);
const dec = setValue.bind(null, value - 1);
return (
<p>
<span>Value is "{value}".</span>
<button aria-label="decrement value" onClick={dec}>-</button>
<button aria-label="increment value" onClick={inc}>+</button>
</p>
);
}