V7: `form.watch(watchObserver)` causes duplicate updates for the same event.
See original GitHub issueDescribe the bug
It seems that every time the form updates, form.watch
creates a new observer, so it starts emitting more and more duplicate events over time.
Codesandbox link (Required)
https://codesandbox.io/s/formwatch-bug-0658y?file=/src/index.js
In this sandbox:
- Edit the fields. You’ll see only one console.log per change event.
- Click the
update
button once or more. - Edit the fields again. Now you’ll see duplicate events per change.
To see the bug circumvented:
- Uncomment the memoized
<Watcher />
and comment the unmemoized one. - Click update a few times.
- Edit the fields. There will only be one event per change.
I’m doing a hard-memoize (no deps), so the Watcher cannot respond to changes. In the example, it works, but there’s numerous circumstances in which this would break.
Expected behavior I expect form.watch to register only the most recent observer, unregistering whatever was in place beforehand. (ref-style).
I tried to make this happen using form.watch(useCallback(observer))
, but I still got the duplicates.
I also considered using a useWatch
in a subcomponent and passing down control
instead of form
, as I believe this would be ideal, however, useWatch
doesn’t support watchObserver
as an argument. (I’ve got another FR coming for this so that you can consider it separately.)
Thanks!
Issue Analytics
- State:
- Created 2 years ago
- Comments:5 (3 by maintainers)
sounds good to me, feel free to contribute to the doc and include that piece of information.
Thanks for the feedback @mikestopcontinues I have pinged one of the main feature request issues. I prefer to keep the issue board clean as you know, we don’t have a lot of people to maintain the repo at the moment. I am filtering all the issues as well for duplication.