-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Description
Prerequisites
- I have searched the existing issues
- I understand that providing a SSCCE example is tremendously useful to the maintainers.
- I have read the documentation
- Ideally, I'm providing a sample JSFiddle, Codesandbox.io or preferably a shared playground link demonstrating the issue.
What theme are you using?
other
Version
5.19.3
Current Behavior
Form
's current shouldComponentUpdate
uses a custom deep equality check that assumes all functions are equal.
This assumption was added for performance reasons, see #255.
This assumption breaks function closures, meaning callback functions are fired from within the form with stale state. See SSCCE example in "Steps to Reproduce".
Expected Behavior
Callback functions are a common React pattern, used to provide dynamic behavior depending on the state of the application. This state is usually encoded in the callback's closure. Any component that takes such a function as a prop should update when the function changes.
A possible solution would be to switch shouldComponentUpdate
to use shallow equality, Object.is()
, which is the standard for most React components.
To address the performance concerns, the typical React pattern to avoid unnecessary component rerenders is to wrap any callback function in a useCallback
.
Steps To Reproduce
Sandbox: https://codesandbox.io/p/sandbox/serene-noether-dx4x7y
Minimal example that shows stale values displayed from a custom field template. Click "increment counter" button, observe the difference in value between calls to getCounterValue inside & outside the form.
Environment
Anything else?
No response