How to prevent unnecessary mutation with useFieldArray? #11525
Unanswered
razb-viola
asked this question in
Q&A
Replies: 1 comment
-
Also problem with this, when you append a new row, you must provide some default value, otherwise typescript will yell on you. |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
This is the schema of my form:
As you can see, business is optional. However, if you actually trying to fill it, you must provide a type, which is mandatory.
Now in order to allow items to be added, I am using the
useFieldArray
. like this:const items = useFieldArray({name: 'business.items', control})
The problem is that by default it adds a
business
object to my form values with a blank array of items in it. Like this:Then when trying to send the form I get errors due to 'business.type' which is mandatory.
Could I tell some how to react hook form no to behave like so, or, the
useFieldArray
to be applied only based on certain conditions?I may render this in a dedicated separate component. However when I unmount the component I should have to clean up the state and revert it to as it was before - manually.
Beta Was this translation helpful? Give feedback.
All reactions