Hook for persisting and rehydrating state in the React app.
npm install react-persist-hook
or
yarn add react-persist-hook
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>
)
}
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 |