sarahdayan / react-persist-hook

Hook for persisting and rehydrating state in the React app

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

React Persist Hook

Hook for persisting and rehydrating state in the React app.

Installation

npm install react-persist-hook

or

yarn add react-persist-hook

Usage

import React from 'react'
import usePersist from 'react-persist-hook'

const Signin = () => {
  const [signinFormValues, setSigninFormValues] = usePersist('signin-form', 'localStorage', 400);

  const { userName, email } = signinFormValues || {};
  const initValues = { userName, email };

  const handleSubmit = ({ userName, email }) => {
    setSigninFormValues({ userName, email });
    // rest of the submit method
  }

  return (
    <form onSubmit={handleSubmit}>
      {/* rest of the code */}
    </form>
  )
}

Props

Prop Type Default Required Description
key string true Unique storage key
storageType ['localStorage'|'sessionStorage'] 'localStorage' false Storage type name
debounceLimit number 250 false Time in milliseconds to debounce the state persisting

License

MIT

About

Hook for persisting and rehydrating state in the React app


Languages

Language:TypeScript 100.0%