Can't seem to update a field when using useFieldArray with setValue
See original GitHub issuePlease consider asking the question at our spectrum channel.
https://spectrum.chat/react-hook-form
Describe the question?
To Reproduce Steps to reproduce the behavior:
- use
useFieldArray
- try to update a certain field in all members of the array using
setValue
fields
is out of sync withwatch
Detailed explanation:
I’m using useFieldArray to manage an array of objects.
One of the fields in these objects has to be set manually, since it affects the other array members (it’s a toggle of who’s the primary member of the array, toggling one as the primary toggles the others off)
Using setValue makes the fields
object out of sync from what comes from watch
.
Issue Analytics
- State:
- Created 3 years ago
- Comments:30 (13 by maintainers)
Top Results From Across the Web
Editing single item in useFieldArray - react hook form
The issue is when I switch between items, nested array values don't seem to update the data correctly. I think I understand that...
Read more >useForm - setValue - React Hook Form
To update the entire Field Array, make sure the useFieldArray hook is being executed first. Important: use replace from useFieldArray instead, update entire ......
Read more >React Hook Form - What's the point of setValue if it doesn't do ...
Edit: It seems like I just can't read and setValue 's API ... This function allows you to dynamically set the value of...
Read more >React Hook Form, 6 months later - Evan Williams
However, our use cases involve down stream updates based on form ... Our inputs' formats can't fully be known for each field to...
Read more >Using Material UI with React Hook Form - LogRocket Blog
That's why inside the render function we are not using the field prop anymore. To set the value we are taking another new...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
I have had a similar problem: I’m using useFieldArray and watch this fields with useWatch to have changes within the fields reflected directly. When I loaded existing values from the database and populated the form via setValue, this didn’t work with array fields. The values have been set via setValue(), but they were directly overwritten due to re-renders and inconsistencies between useFieldArray fields and useWatch.
I changed my code the following way and it works now:
<Controller>
component for field array fields (because they are not registered yet). I had default values for these input and when they got mounted, they were overwriting the loaded value from the database.useFieldArray
anduseWatch
to get a controlled field array. the returned fields fromuseFieldArray
should be used as defaultValue for useWatch, e.g. `useWatch({ name: ‘parameters’ , defaultValue: fields})``reset({ ... })
instead ofsetValue()
I’m using
"react-hook-form": "^7.6.3"
.I have the same issue in nested field arrays. The top-level field array doesn’t work properly, but the second one is good with fields.