final-form / react-final-form

🏁 High performance subscription-based form state management for React
https://final-form.org/react
MIT License
7.39k stars 481 forks source link

<FormSpy /> onChange fires several times when fields are added to the form conditionally and in an iteration #739

Open gaborluk opened 4 years ago

gaborluk commented 4 years ago

Are you submitting a bug report or a feature request?

Bug report.

What is the current behavior?

I was writing a form generator component when I ran into this issue. It seems like the onChange handler of <FormSpy /> fires way too many times during the rendering of the form if some of the fields are added in an iteration, and their rendering is limited by a condition.

It's much easier to demonstrate the issue than to describe it, so I have created a CodeSandbox with an attempt to reasonably isolate the issue.

Some things to note:

* By originally known fields, I mean the fields that have been ready to be rendered since the very first render cycle, not being blocked by a falsy condition initially.

I'm not under the impression that I understand the issue well, but I have observed a few things:

I hope this information is sufficient for reproducing and fixing the issue.

What is the expected behavior?

<FormSpy />'s onChange handler fires only once per form, as opposed to gradually firing when a new field is internally encountered, multiple times for each.

Sandbox Link

https://codesandbox.io/s/serene-sammet-0odqq

What's your environment?

React Final Form 6.3.5, Final Form 4.18.7, Windows, Chrome.

brionmario commented 1 year ago

Any update on this? I'm also having the same issue in "react-final-form": "^6.5.3",.