DefaultValues with FieldArray #11580
Unanswered
orixjintaokoong
asked this question in
Q&A
Replies: 2 comments
-
My suspicion is the |
Beta Was this translation helpful? Give feedback.
0 replies
-
If you are appending a new array item, you should provide default values for each field in the object, as opposed to an empty object. From
- fields.append({})
+ fields.append({
+ name: "",
+ age: ""
+ }) |
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.
-
The problem I'm solving
A CRUD application with prefilled forms after getting from an API from a promise, users can remove and add items into the array dynamically and update the item via an API on submit.
Current approach
After getting a response, I will render a form and set the values as the
defaultValues
in the option foruseForm
.The issue
After setting as default, when I remove one of the existing item from the array and adding a new one, the newly added item which I expect to have all empty values, now are prefilled with the previously deleted item's values.
Workaround
To use some sort of onMount logic to set the values of the form using
form.setValue("fields", data.fields")
. However, this can get complex as the form grows.Codesandbox (reproduce)
codesandbox
Beta Was this translation helpful? Give feedback.
All reactions