Skip to content

Fix form fields set to blank when changing watched values

Loic Huder requested to merge fix-blank-fields into master

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 and noReturnAddress), 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 the FormContext (https://react-hook-form.com/advanced-usage#FormProviderPerformance). In short, changes in the form state will trigger re-renders of TextFieldGroup
  • 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

Merge request reports