watch
Watches for changes in form state and recomputes a derived value.
This function uses derivedStateOf
internally, which can be expensive for frequent recompositions. It's primarily intended for cases where you need to synchronously update field states based on input values, such as enabling/disabling fields based on other field values.
For simple conditions with infrequent recompositions, consider using the form state directly instead of this watch function to avoid unnecessary overhead.
Usage:
// Enable password confirmation field only when password is not blank
val isPasswordConfirmationEnabled = form.watch { value.password.isNotBlank() }
form.PasswordConfirm(
name = "pwd-cfm",
dependsOn = setOf("pwd"),
enabled = isPasswordConfirmationEnabled
) {
// field implementation
}
// Enable submit button based on multiple field conditions
val canSubmitForm = form.watch {
value.email.isNotBlank() && value.password.length >= 8
}
// Show conditional fields based on user selection
val showAdvancedOptions = form.watch { value.userType == "advanced" }
Content copied to clipboard
Return
The computed value that updates when form state changes.
Parameters
T
The type of the form data.
R
The type of the computed result.
calculation
A function that computes a value based on the current form state.