@hoeks/use-persisted-state
TypeScript icon, indicating that this package has built-in type declarations

1.0.2 • Public • Published

@hoeks/use-persisted-state

The hoeks usePersistedState hook. Persist your state variable asynchronously. By default it uses IndexedDB. Made with TypeScript

Installation

$ yarn add @hoeks/use-persisted-state
$ npm i --save @hoeks/use-persisted-state

Usage

It's similar to the React.useState hook. The only difference is that you provide a second argument being the key which will be use to save the state in IndexedDB. It also returns an extra function to clear the state from IndexedDB and a 4th state indicating if the state has been hydrated

Example

import * as React from 'react';
import usePersistedState from '@hoeks/use-persisted-state';

const Example: React.FunctionComponent = () => {
  const [count, setCount, clear, isHydrated] = usePersistedState(1, 'example');

  if (isHydrated) return <p>Loading...</p>;

  return (
    <>
      <p>You clicked {count} times</p>
      <button onClick={() => setCount(count + 1)}>Click me</button>
      <button onClick={clear}>Clear me</button>
    </>
  );
};

Dependencies (1)

Dev Dependencies (0)

    Package Sidebar

    Install

    npm i @hoeks/use-persisted-state

    Weekly Downloads

    1

    Version

    1.0.2

    License

    MIT

    Unpacked Size

    26.5 kB

    Total Files

    15

    Last publish

    Collaborators

    • onursagir