1.0.5 • Published 5 years ago

@jsxtools/use-local-storage-factory v1.0.5

Weekly downloads
2
License
CC0-1.0
Repository
github
Last release
5 years ago

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>
  );
}
1.0.2

5 years ago

1.0.5

5 years ago

1.0.4

5 years ago

1.0.3

5 years ago

1.0.1

6 years ago

1.0.0

6 years ago