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.

field array loses values using Controller

See original GitHub issue

To Reproduce Steps to reproduce the behavior:

  1. Go to https://codesandbox.io/s/react-hook-form-usefieldarray-nested-arrays-forked-78qdb?file=/src/fieldArray.js:665-670
  2. Click “Append”, enter any text
  3. Click “hide” button and then “show”
  4. See default values in text input

If I use register instead of Controller everything is fine.

Codesandbox link (Required) https://codesandbox.io/s/react-hook-form-usefieldarray-nested-arrays-forked-78qdb?file=/src/fieldArray.js:665-670

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:3
  • Comments:8 (4 by maintainers)

github_iconTop GitHub Comments

5reactions
bluebill1049commented, Apr 25, 2021

I am going to roll back this fix guys, as this is breaking a deeply nested field #4941 https://github.com/react-hook-form/react-hook-form/issues/4941, so to work around this issue.

let me explain the problem because useFieldArray needs defaultValue as the initial input state. Toggling input doesn’t really change the defaultValue stored in useFieldArray resulted in the above issue.

https://codesandbox.io/s/react-hook-form-usefieldarray-nested-arrays-forked-31soo

so work around this issue for toggling input, you can use getValues to set the defaultValue, well I know this is not convenient, but it’s about to cover all use cases, and I hope for your understanding. also you can use useWatch API for the individual’s defaultValue as well.

https://codesandbox.io/s/react-hook-form-usefieldarray-nested-arrays-forked-ez7un?file=/src/fieldArray.js

I have also included a new section in the doc:

Screen Shot 2021-04-25 at 1 36 29 pm
0reactions
shestakov-vladyslavcommented, Sep 16, 2022

This is very unobvious 🙁 And I can’t find this information in documentation

Read more comments on GitHub >

github_iconTop Results From Across the Web

React-hook-form field value get lost when i collapse, add or ...
I am trying to add new panels from Add button click and remove panel from Remove button click, its working fine, problem is,...
Read more >
useFieldArray - Simple React forms validation
Performant, flexible and extensible forms with easy-to-use validation.
Read more >
10 Most Common Mistakes That PHP Developers Make - Toptal
After going through the first foreach loop, $array remains unchanged but, as explained above, $value is left as a dangling reference to the...
Read more >
SANtricity 11.30 Maintaining a Storage Array Using CLI
The CLI command wrapper is a shell that identifies storage array controllers, embeds operational terminals, embeds script commands, and passes these values to ......
Read more >
2080-UM002M-EN-E Micro830, Micro850, and Micro870 ...
Micro850 Controller Catalog Numbers 2080-LC50-24AWB, 2080-L50E-24AWB, ... you need to remove power from all field devices, you must include MCR.
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