You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
React hook for save data from react-hook-form to local/session storage (typescript friendly)
Usage
import{useForm}from"react-hook-form";importuseFormDataToStoragefrom"../hooks/useFormDataToStorage";import{Person,validationSchema,valuesTypes}from"./yupSchema";import{yupResolver}from"@hookform/resolvers/yup";const{
register,
handleSubmit,
reset,
watch,
setValue,formState: { errors },}=useForm<Person>({resolver: yupResolver(validationSchema)});constclearStorage=useFormDataToStorage<valuesTypes>({
watch,
setValue,validate: false,// set shouldValidate (default false) // see more on https://react-hook-form.com/api/useform/setvalue/ dirty: false,// set shouldDirty (default false)storage: localStorage// set storage local/session (default Local)storageKey: "formData",// set storage keyfieldsToBeSaved: ["title","firstName","lastName","email"],// set fields which be saved to storageclearStorageOnUnmount: false,// clear storage on component unmount (default false)});functiononSubmit(data: Person){alert(JSON.stringify(data,null,4));clearStorage();reset();}