react-persist-hook
v1.0.7
Published
Hook for persisting and rehydrating state in React app
Downloads
20
Readme
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 |