Future Export: useFormData and isDirty
#973
Replies: 4 comments 26 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. |
|
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';
}); |
|
Does this hook Let's say that I'm building a realtime editor of sales pages. I want the user to create custom fields. Each field is of type {
type: "checkbox" | "input" | "radio";
options: string[];
defaultValue: string;
}And with each option the user adds (using intents) I want to display that somewhere else. I'd like to not have to sync this with external state manager but rather simply subscribe to that value somewhere else (still inside FormProvider though). As of now I'm using const [field] = useField('name');
// field.value |
|
Is there anyway to have |

Uh oh!
There was an error while loading. Please reload this page.
Today we are introducing two new experimental APIs under the
futureexport:useFormDataandisDirty. 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.
useFormDataThis is a low-level React hook that lets you derive a value from the current
FormDataof 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
isDirtyThis 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
useFormDataSubmit 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
FormDataobject against thedefaultValueyou 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
skipEntryoption 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 likeuseFormDataandisDirtywill 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!
All reactions