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

FormRenderProps inconsistant with FormState after unregistering/re-registering a field #855

Open stevechen995 opened 4 years ago

stevechen995 commented 4 years ago

Are you submitting a bug report or a feature request?

Bug Report

What is the current behavior?

When unregistering/re-registering a field on a form that is subscribed to pristine, the pristine value provided by FormRenderProps is different from the "true" pristine value in formState.

sandbox link: https://codesandbox.io/s/react-final-form-simple-example-forked-q8p7o?file=/index.js

steps to reproduce:

  1. make changes to the "Click Swap after edit" input ONLY
  2. click the swap button
  3. observe the prop pristine value(true) is different from the real pristine value(false) (indicated by the submit button being greyed, also printed below)

What is the expected behavior?

Expect the latest pristine value passed as a FormRenderProp to equal the true pristine value in formState, as we are subscribed to pristine

Sandbox Link

https://codesandbox.io/s/react-final-form-simple-example-forked-q8p7o?file=/index.js

What's your environment?

Other information

Ran into this issue when we had 2 mutually exclusive components to deal with a single field, this problem would pop up when switching between the two if no other field is touched. This issue can be troublesome if you're only subscribed to pristine, since the form won't re-render until the pristine value changes again. In the example above, after clicking swap, even changing the unrelated field won't do anything, because the internal state already has pristine: false, so it won't re-render.

A workaround is to keep the field constantly registered somewhere else on the form. When you unregister a field, it's no longer taken into account for pristine calculations, so thats probably whats causing the pristine value to flip back to true in the first place. However, re-registering it with the same value does seem to trigger another pristine calculation, but it isn't causing a re-render the second time.

sathishkumar294 commented 2 years ago

Any updates on this issue? Even I am facing a similar problem. Here is a simpler, reproducible sandbox link: https://codesandbox.io/s/delicate-dawn-h0knu?file=/src/SampleForm.jsx