Future Export: useFormData
and isDirty
#973
edmundhung
announced in
Announcements
Replies: 2 comments 12 replies
-
Do you think it would be possible to make the values of single static fields more easily accessible? It seems weird to have to parse the FormData myself if I have already set up the schema in the useForm call. Especially for multi value fields, and for nested objects/arrays, it seems very cumbersome and error prone to work on the FormData manually. |
Beta Was this translation helpful? Give feedback.
9 replies
-
Is it possible to use hidden field as formRef for const control = useControl({
defaultValue: field.defaultValue,
});
<input
type="hidden"
name={name}
ref={control.register}
/>
const checked = useFormData(control.register, (formData) => {
return formData?.get(fields.services.name) === 'on';
}); |
Beta Was this translation helpful? Give feedback.
3 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
Today we are introducing two new experimental APIs under the
future
export:useFormData
andisDirty
. These APIs provide a more flexible way to respond to form state changes, with minimal coupling to the form itself.These additions let you extend the form in your own direction—without relying on built-in state. Just observe what you care about, and compute only when needed.
useFormData
This is a low-level React hook that lets you derive a value from the current
FormData
of a form, and re-renders your component only when that value changes.It works by listening to native form events (
input
,focusout
,submit
,reset
) and DOM mutations—so it stays in sync even when fields are dynamically added, removed, or renamed.You can use this to:
Render live previews or computed summaries
Toggle sections of the UI based on field values
Track form state from anywhere, even outside the form
Read the full API Reference
isDirty
This is a utility function that compares the current form values with the default values and tells you whether anything has changed.
You can use it to:
Track the dirty state of a form in combination with
useFormData
Submit the form only if the form is dirty
Process form submission on the server only if the form is dirty
The function compares the full
FormData
object against thedefaultValue
you provide. This is not always the same as the initial values of the form, especially if the form includes hidden fields used for server-side processing or security purpose, such as CSRF tokens.To avoid false positives in these cases, you can use the
skipEntry
option to ignore specific fields during comparison:Read the full API Reference
Why these APIs?
While similar functionality is available today through
useForm
, these new primitives are designed to decouple form state from core logic, giving you more control over how and when to derive what you need.They also lay the foundation for upcoming changes to
useForm
, which will adopt a simpler state model and reduce built-in subscriptions. Going forward, APIs likeuseFormData
andisDirty
will allow you to reintroduce those behaviors only where you need them, without the overhead of tracking everything by default.These APIs are still experimental and may evolve before being promoted to stable. If you try them out, we would love to hear your feedback. Let us know what you build!
Beta Was this translation helpful? Give feedback.
All reactions