question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Can't seem to update a field when using useFieldArray with setValue

See original GitHub issue

Please consider asking the question at our spectrum channel.

https://spectrum.chat/react-hook-form

Describe the question?

To Reproduce Steps to reproduce the behavior:

  1. use useFieldArray
  2. try to update a certain field in all members of the array using setValue
  3. fields is out of sync with watch

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:closed
  • Created 3 years ago
  • Comments:30 (13 by maintainers)

github_iconTop GitHub Comments

3reactions
zirkelccommented, May 29, 2021

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:

  • provide no default values via <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 and useWatch to get a controlled field array. the returned fields from useFieldArray should be used as defaultValue for useWatch, e.g. `useWatch({ name: ‘parameters’ , defaultValue: fields})``
  • after data has been loaded, populate the form with reset({ ... }) instead of setValue()

I’m using "react-hook-form": "^7.6.3".

2reactions
dancixxcommented, Jul 7, 2021

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.

Here doesn't work the fields.map just the watched returns.
const { control, watch, getValues } = useFormContext();
const { fields, remove } = useFieldArray({
            control,
            name: 'exercises',
        });
const watchedExercises: Array<WorkoutExerciseProps> = watch('exercises');

Here works the fields.map method.
const { control, watch } = useFormContext();
const { fields, append, remove } = useFieldArray({
        control,
        name: `exercises.${exerciseIndex}.exerciseSets`,
    });
Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found