npm.io
1.0.3 • Published 6 years ago

react-use-persistent-state

Licence
—
Version
1.0.3
Deps
1
Size
5 kB
Vulns
0
Weekly
0

A react hook that stores your states to LocalStorage. It uses the localforage module to get and set the value.

Example:

const TestComponent = () => {
  const defaultValue = 1;
  const [value, setValue, hydrated] = usePersistentState(
    "storageKey",
    defaultValue
  );
  return <div onClick={() => setValue(value + 1)}>{value}</div>;
};

The hydrated variable is to check whether the hook has retrieved the stored value from LocalStorage on initial mount.