Skip to content

Form does not rerender when formContext functions change #4690

@robbieculkin

Description

@robbieculkin

Prerequisites

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions