Computed fields
Added in v0.17.
compute derives a field's value from other values. It is re-run when a value it reads changes, and its return type is checked against the field's type. A new compute function also re-runs it, so a compute that closes over something outside the form (the current language, a fetched rate) follows it when the config is rebuilt.
The computed value is written into the form state like any other value: it is displayed by the field component, validated by rules or schema, reported to onFieldChange, and submitted.
Arguments
compute(values, ctx) receives the same arguments as condition functions:
Return undefined when the inputs are incomplete. Returning a value of another type is a type error:
Dependencies
Like conditions, the paths read through values / ctx.scope are recorded on every run, so a compute re-runs only when one of them changes. Typing in an unrelated field neither runs the function nor re-renders anything. Declare deps to override the inferred paths.
Chains and aggregates
A computed field can read other computed fields: each one re-runs when its inputs change, in any config order.
In array rows, use ctx.scope for the row, and aggregate rows at the top level:
Typing in one row re-runs only that row's total and the aggregate. Appending, removing and moving rows re-run what depends on them.
Editable vs read-only fields
There is no option to configure. The behavior follows whether the user can edit the field:
readOnly/disabled(field or form level): the field always shows the computed value, even over a stale saved value (e.g. BMI).- Editable (e.g. a slug): the field follows the computed value until the user changes it, then keeps the user's value. Clearing the field resumes syncing on the next change. On an edit form, a saved value that differs from the computed one counts as a user change and is kept.
Pristine forms
Values computed on mount and after reset() become the field's default value, so the form is not dirty just because a computed field was filled in.
Cycles
Computes that depend on each other in a loop (a from b, b from a) are stopped, with a [react-headless-form] compute cycle detected error listing the paths. Mutually dependent computes that settle on a stable value are fine.
Notes
computeis synchronous. Fetch remote data elsewhere and store it in a field thatcomputereads.- A field hidden with
visiblekeeps computing (its value stays current and is submitted unlessshouldUnregisteris set). Fields inside a hidden section are unmounted and do not compute.
Examples
- Computed fields: full name, initials and BMI.
- Integration demos: a registration total computed from the ticket and the guest list, in twelve UI libraries.

