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.

watch vs. useWatch array length inconsistency when using useFieldArray

See original GitHub issue

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:

  1. Go to the CSB link, clear the console after render.
  2. Click on “insert” or “remove”.
  3. See the console. Check the length of the fields vs. the length of the useWatchedItems on 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. 👍

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:15 (8 by maintainers)

github_iconTop GitHub Comments

2reactions
bluebill1049commented, Apr 18, 2021

Ah ok, thanks. So you’re saying this is expected, right? 😃

Yes, the last re-render will bring the updated field values.

2reactions
bluebill1049commented, Nov 8, 2020

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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

useFieldArray - Simple React forms validation
Custom hook for working with Field Arrays (dynamic form). The motivation is to ... You can watch this short video to visualize the...
Read more >
react-hook-form - Awesome JS
fix(useWatch): default value for array of inputs (#9555) fix Controller example using ... fix #9134 useFieldArray validation action inconsistency (#9136)
Read more >
How to make a dynamic watch()/usewatch() with react-hook ...
length === 0 || watchThese.fullName}/> which works but is obviously triggered by every field. How could I generate a dynamic watch()/useWatch() ...
Read more >
react-hook-form: Versions - Openbase
fix #9134 useFieldArray validation action inconsistency (#9136) close #9126 ... useController by integrating with useWatch ✌ package size-reduction ...
Read more >
React Hook Form - useFieldArray - YouTube
In this session, we are taking a look at the useFieldArray custom hook to manage your dynamic form fields.working example: ...
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