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.

setValue not working for arrays with controller

See original GitHub issue

Describe the bug I wonder if this bug is related to https://github.com/react-hook-form/react-hook-form/issues/5232 as the issues I usually see are related to non-primitive values, namely arrays.

To Reproduce

  1. Use controller interface instead of register to track fields in your form
  2. Add a manually tracked field via defaultValues
  3. Add a callback modifying the manual field and call it (in my example by pressing the “Remove” button)
  4. Press the “submit” button

Codesandbox link (Required) https://codesandbox.io/s/dark-voice-glnie?file=/src/App.tsx

Expected behavior The submitted values are

{
  "array": ["a"],
  "firstName": "alll"
}

Actual behavior The submitted values are

{
  "array": ["a", "b"],
  "firstName": "alll"
}

Issue Analytics

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

github_iconTop GitHub Comments

3reactions
mrluboscommented, Jun 21, 2021

@mrlubos I trimmed it down to this repro here: https://codesandbox.io/s/react-hook-form-react-select-u36uv

It’s actually not behaving like the real app where I see always an empty array but I see another weird behaviour. Removing all items from the select… 🤔

  • leaves 1 item in the watched values
  • is undefined in submit values

@damianobarbati Are you able to upgrade react-select to version 4.0.0 or newer? Doing so fixes the issues you mention above. Can you verify if that would also resolve the other issues you’re seeing?

2reactions
damianobarbaticommented, Jun 21, 2021

@mrlubos I see it working properly with the latest react-select. I’ll ping if I manage to reproduce the real app behaviour, thank you!

Read more comments on GitHub >

github_iconTop Results From Across the Web

setValue doesn't work when field array name changed · Issue ...
It seems that if a field array item has its name changed and then setValue is used to try and update the name,...
Read more >
React hook form method - setValue - doesn't work
I have some clearable select, and I want to reset the applets field in state to an empty array ...
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 >
6 Different Ways to Set Values for Reactive Forms | by Atit Patel
In this article, I am covering all the methods which can be used to set values for single form control or form. 1....
Read more >
Array.SetValue Method (System) - Microsoft Learn
If SetValue is used to assign null to an element of an array of value types, all fields of the element are initialized...
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