react-hook-form: watch vs. useWatch array length inconsistency when using useFieldArray
Describe the bug
Not sure if this is a bug/can be improved but… when using useFieldArray to insert/remove item to the fields, the resulting output (array) of that useWatch doesn’t have a consistent length with the array length of updated fields after the insertion/removal.
To Reproduce Steps to reproduce the behavior:
- Go to the CSB link, clear the console after render.
- Click on “insert” or “remove”.
- See the console. Check the length of the
fieldsvs. the length of theuseWatchedItemson the first render loop.
Codesandbox link (Required) https://codesandbox.io/s/watch-vs-usewatch-field-array-length-inconsistency-bi8it?file=/src/App.jsx:1365-1650
Expected behavior
The length of the fields and the length of the useWatchedItems should be the same on each render cycle.
Additional context
When watch is used instead of useWatch though, the watched array length seems to be consistent with the fields array length on each render. 👍
About this issue
- Original URL
- State: closed
- Created 4 years ago
- Comments: 15 (8 by maintainers)
Commits related to this issue
- #2990 remove warning with defaultValue at Controller close #3329 useWatch return value to skip extra re-render — committed to react-hook-form/react-hook-form by bluebill1049 4 years ago
- 👨🏻💻 #2990 remove warning with defaultValue at Controller (#3397) * #2990 remove warning with defaultValue at Controller close #3329 useWatch return value to skip extra re-render * fix useWatc... — committed to react-hook-form/react-hook-form by bluebill1049 4 years ago
Yes, the last re-render will bring the updated field values.
Previously its a bug because it didn’t return the correct wath value, i will do some investigation with inconsistent watch value. I know the root cause, because useWatch need to trigger another setState to update the value, but worth to do some investigation, if not working then will close this issue.
Ohh, didn’t expect that you’d go the extra mile for this. Was actually already quite satisfied with some workaround, like:
But it’s a great feeling to expect that I’m gonna be able to do this:
Your effort is most appreciated, Bill – as always – thank you soo muucch! 😃
I wouldn’t say it can’t be improved, but i did try last night and it’s not working out…
because
watchgets called even before the child component. order below:watchuseWatch