tiaanduplessis / react-hook-form-persist

Persist and populate react-hook-form form using storage of your choice
MIT License
168 stars 23 forks source link

fix: add a cleanup function to useEffect that loads data from storage. #29

Open mkhennoussi opened 2 years ago

mkhennoussi commented 2 years ago

Hi !

This fixes the issue with data not loaded from the storage as the Effects are fired twice in dev: https://beta.reactjs.org/learn/synchronizing-with-effects#how-to-handle-the-effect-firing-twice-in-development

It simply creates a cleanup function that wirte to the storage after reading it.

Should fix: #18

Hope it helps !

BorjaRafolsMartinez commented 1 year ago

This actually makes this plugin work with defaultValues option from react-hook-form

Alain1405 commented 1 year ago

I can confirm with this fix the form values are persisted even when defaultValuea is passed to useForm. Without this defaultValues overrides the persisted state. An alternative solution would be to pass defaultValues directly to useFormPersist