Form not populating with defaultState after re-render #2282
-
CodeSandbox: https://codesandbox.io/s/react-hook-form-reset-demo-fg9f3?file=/src/index.js In the linked toy example, an API call to return some default user name values is simulated. The values are stored in a state atom called userInputState, which is passed down to a child form (using react-hook-form) as "initialState." The form uses initialState for defaultValues, and when it is changed a useEffect calls reset() on the data. This works for the first mounting of the form. After submission, the form state is updated and you are taken to a new component view. The form state looks correct by this point. However, if you click the BACK button and the first page with the form is shown a second time, the (new) default values do not populate. Any assistance in correctly using defaultValues and reset would be appreciated. Thanks in advance. |
Beta Was this translation helpful? Give feedback.
Replies: 8 comments 11 replies
-
|
Beta Was this translation helpful? Give feedback.
-
ok, let me explain:
I recommend you build a simple codesandbxo with above behaviour first. |
Beta Was this translation helpful? Give feedback.
-
In my case, I was populating an object with default values at |
Beta Was this translation helpful? Give feedback.
-
Hi @bluebill1049, I may be missing something here but is it possible to refresh the cache for defaultValues? When my component re-renders on subsequent renders I would like the form to populate with the new prop values but not to trigger a i.e watch is only triggered on user changes but the form is kept in sync with prop values |
Beta Was this translation helpful? Give feedback.
-
I still feel like this question isn't answered.....or I feel as though this needs better DX. Just spent way too long trying to figure out why I couldn't submit one form, then submit another. Turns out you either bite the full page refresh bullet or you have to call form.reset({
// all form values here
}) This works fine for small forms I guess, but definitely needs some improvement. defaultValues should be used when |
Beta Was this translation helpful? Give feedback.
-
I don't think above solutions adequately answer the problem, the best way to deal with this issue is by using The Check this documentation example for more details. |
Beta Was this translation helpful? Give feedback.
-
How to handle the case when the data is fetched using rtk query and am feeding the function into default values. Default values does support async operations, right? |
Beta Was this translation helpful? Give feedback.
-
How does react hook form updates when async default values are fetched successfuly? |
Beta Was this translation helpful? Give feedback.
ok, let me explain:
defaultValues
useEffect
withreset
to update the formdefaultValues
I recommend you build a simple codesandbxo with above behaviour first.