Using ternary operator causes the fields to get incorrect defaultValue
See original GitHub issueHey, First off, thanks for open sourcing this library, I really appreciate it.
I encountered this issue that I’m not sure if it should be solved by the library, or if it can just be documented.
Codesandbox link (Required) https://codesandbox.io/s/loving-dew-7i24k
To Reproduce
- Toggle the switch
- Get an error
In the sandbox, you will see that I have a ternary operator that switches between 2 input fields (using controllers).
The first input field expects an array, while the other expects an object. An error is thrown when you toggle between the 2 fields because React doesn’t seem to rerender with the correct defaultValue.
I managed to solve it by either:
- Giving each of the controllers a key prop (Uncomment them and refresh the sandbox to fix it)
- Using the
&&operator instead of ternary. This causes the controllers to unmount completely instead of rerendering.
I think you can solve #1135 the same way.
Again, I’m not sure if you think the responsibility is on react-hook-form. If not, thanks again for writing this library 😃
Issue Analytics
- State:
- Created 4 years ago
- Reactions:1
- Comments:8 (5 by maintainers)

Top Related StackOverflow Question
I think it’s fine, but maybe adding an example would help?
Here’s a trimmed down example of my codesandbox, you could edit it to your liking and add it if you want.
Cheers!
let me know if you think it make sense and feel free to adjust and add your words too. thanks