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.

Bug - setValue with field array not working when same id is used

See original GitHub issue

Describe the bug If we use setValue to change a field array and use the same id for an element, then the element will briefly change but then revert back to the original value.

To Reproduce Steps to reproduce the behavior:

  1. Go to the sandbox
  2. Click on change value
  3. Notice in the console that the new value for name is printed but then the original value is printed and UI does reflects the original as it’s the final state.

Codesandbox link (Required) https://codesandbox.io/s/funny-mendel-xgo9y?file=/src/App.js

Expected behavior When using setValue with the same id for elements, I would expect formValues to still be updated. Using the same id becomes important not only for performance but also for preserving the state of field array elements. Imagine that we have something like:

{fields.map((item, index) => (
  <ComplexInput
     key={item.id}
     item={item}
   />

<ComplexInput/> might have multiple inputs and its own internal state (like data fetched from a server) that we want to preserve upon changing values with setValue. The only way to do this is to use the same id for key.

Screenshots Screenshot from 2021-05-07 17-08-26

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:9 (9 by maintainers)

github_iconTop GitHub Comments

2reactions
leapfulcommented, May 8, 2021

@wdfinch

If your child component of field array always has the index, you can set the value directly by using its index, for example:


setValue(`names.${index}.name`, 'mike');

Working sample: https://codesandbox.io/s/strange-wright-8evmy?file=/src/App.js

0reactions
wdfinchcommented, May 8, 2021

I’m noticing a potential bug but will make a new issue.

Read more comments on GitHub >

github_iconTop Results From Across the Web

V7 possible bug - setValue with field array #4368 - GitHub
The issue is it seems like setValues does not update formValues correctly even for connected field arrays. I have two mounted field arrays....
Read more >
react-hook-form's setValue method is not working if input is in ...
I have tried to use useState to control the input value, but there is another problem. When clear the input, click submit button,...
Read more >
useFieldArray - Simple React forms validation
Performant, flexible and extensible forms with easy-to-use validation.
Read more >
Google Visualization API Reference | Charts
This parameter is an array of cell values: if you only want to specify a value for a cell, just give the cell...
Read more >
FormArray - Angular
Tracks the value and validity state of an array of FormControl ... FormArray is one of the four fundamental building blocks used to...
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