Persist and populate react-hook-form form using storage of your choice
npm install react-hook-form-persistantPersist and populate react-hook-form form using storage of your choice
- react-hook-form-persistant
- 📖 Table of Contents
- ⚙️ Install
- 📖 Usage
- Additional examples
- 📚 API
- 💬 Contributing
- 🪪 License
Install the package locally within you project folder with your package manager:
With npm:
``sh`
npm install react-hook-form-persistant
With yarn:`sh`
yarn add react-hook-form-persistant
With pnpm:`sh`
pnpm add react-hook-form-persistant
`jsx
import React from "react";
import ReactDOM from "react-dom";
import { useForm } from "react-hook-form";
import useFormPersist from 'react-hook-form-persistant'
function App() {
const { register, handleSubmit, watch, errors, setValue } = useForm();
useFormPersist("storageKey", {
watch,
setValue,
storage: window.localStorage, // default window.sessionStorage
exclude: ['baz']
});
const onSubmit = data => {
console.log(data);
};
return (
const rootElement = document.getElementById("root");
ReactDOM.render(
`
Persist all form fields:
`js`
useFormPersist('form', {watch, setValue});
Persist all form fields except password:
`js`
useFormPersist('form', {watch, setValue, exclude: ['password']});
Persist only the email field:
`js``
useFormPersist('form', {watch, setValue, include: ['email'] });
For all configuration options, please see the API docs.
Got an idea for a new feature? Found a bug? Contributions are welcome! Please open up an issue or make a pull request.