Fix form fields set to blank when changing watched values
Fix #392 (closed)
The number of line changes is absolutely not related to the brain power I put in this one.
My analysis goes like this:
- As some fields as watched in the form (ex:
storageConditions
andnoReturnAddress
), changing these fields will change the form state (in the React sense of the term)> - The nested component
TextFieldGroup
subscribes to form state changes as it uses theFormContext
(https://react-hook-form.com/advanced-usage#FormProviderPerformance). In short, changes in the form state will trigger re-renders ofTextFieldGroup
- Somehow, the value of the
TextFieldGroup
is kept in the form state but the re-render of the component sets it to a blank value.
My fix was then to retrieve the value of the TextFieldGroup
from the form state and set it as the defaultValue
of the TextFieldGroup
so that it is restored on re-rendering.
Edited by Loic Huder