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
In my project, I need to remove all empty strings from form results. For example, if a user types something and then deletes it, that empty string should be converted to undefined.
To do this, I decided to update my input component, which uses the Controller strategy, to check if the value is an empty string and, if so, set it to undefined.
That was working well, until an specific implementation of the forms into the project. We have some forms divided into steps, so those forms are using the FormProvider to read/store values. I have a conditional rendering logic to decide what step are active and the things starts to get weird when I change to the second step. If I delete a input value to be empty string, my component will change it to undefined and set this value for that field on the context, but visually that input will have its value equal to the initial value that was set to that field when the component was rendered.
I am not using the defaultValues option of the useForm hook, when my component renders I use setValue to set each field value to the initial value that I want, this way I could avoid the behavior of setting undefined for the field value and the defaultValue be applied.
I wonder why the form context value are correct and on the screen the value switches from empty string to the input initial value and also why this only happens after changing the first step.
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
Problem
In my project, I need to remove all empty strings from form results. For example, if a user types something and then deletes it, that empty string should be converted to
undefined
.To do this, I decided to update my input component, which uses the Controller strategy, to check if the value is an empty string and, if so, set it to
undefined
.That was working well, until an specific implementation of the forms into the project. We have some forms divided into steps, so those forms are using the
FormProvider
to read/store values. I have a conditional rendering logic to decide what step are active and the things starts to get weird when I change to the second step. If I delete a input value to be empty string, my component will change it toundefined
and set this value for that field on the context, but visually that input will have its value equal to the initial value that was set to that field when the component was rendered.I am not using the
defaultValues
option of theuseForm
hook, when my component renders I usesetValue
to set each field value to the initial value that I want, this way I could avoid the behavior of setting undefined for the field value and thedefaultValue
be applied.I wonder why the form context value are correct and on the screen the value switches from empty string to the input initial value and also why this only happens after changing the first step.
Repository
I have a GitHub repo with a sample project so you guys can see what is happening
Running the project
npm install
npm run dev
Replicate the problem
Next
buttonCity
inputundefined
Beta Was this translation helpful? Give feedback.
All reactions