1.0.7 • Published 4 years ago

react-persist-hook v1.0.7

Weekly downloads
11
License
MIT
Repository
github
Last release
4 years ago

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

PropTypeDefaultRequiredDescription
keystringtrueUnique storage key
storageType'localStorage'|'sessionStorage''localStorage'falseStorage type name
debounceLimitnumber250falseTime in milliseconds to debounce the state persisting

License

MIT

1.0.7

4 years ago

1.0.6

4 years ago

1.0.5

5 years ago

1.0.4

5 years ago

1.0.3

5 years ago

1.0.2

5 years ago

1.0.1

5 years ago

1.0.0

5 years ago