Form takes its data as initialFormData or formData
Detect
- A
formData=orinitialFormData=prop onFormor a themed form in your code. - In development, the console shows a warning ending in
[form-data-both-seeds]for a form passed bothformDataandinitialFormData.
Change
Form takes its data through one of two props, as it did in v6 and as an <input> takes value or defaultValue:
initialFormDatais the seed, likedefaultValue. The form reads it when it mounts and whenreset()is called, and fills it in with the schema's defaults. On every other render the form ignores it. After mount, the data belongs to the form. Each edit is applied, validated and shown beforeonChangereports it, whatever your handler does with the event.formDatais the form's data. The form shows what you pass when it mounts, filled in with the schema's defaults, and again whenever you pass another value. An edit is shown as it is made and reported throughonChange. Pass back what the event reported and nothing changes. Pass another value and the form shows that one. Pass the value you passed before, or a copy of it, and the edit stays shown, see A form passed formData shows each new value you pass.getFormData()on the form's ref reads the current value at any time.
Only undefined means "not passed". null shows the schema's defaults, as undefined does. false, 0 and '' are values.
Pass one of the two props. A form passed both takes formData and ignores initialFormData, and in development logs the form-data-both-seeds warning, which setWarningOptions() can make throw.
-<Form schema={schema} validator={validator} initialFormData={record} formData={data} onChange={(e) => setData(e.formData)} />
+<Form schema={schema} validator={validator} formData={data} onChange={(e) => setData(e.formData)} />
A form that passes formData and stores nothing, reading the data on submit or through getFormData(), works as it is. initialFormData reads the same and says what the form does with it:
-<Form schema={schema} validator={validator} formData={record} onSubmit={save} />
+<Form schema={schema} validator={validator} initialFormData={record} onSubmit={save} />
These patterns need no change:
- Storing every edit as it is reported, as in
formData={data} onChange={(e) => setData(e.formData)}. - Writing
formDatainline, such asformData={{}}, without storing whatonChangereports. A re-render no longer throws the user's edits away. - Passing a
formDatathat changes after mount: a record that loads, the next record, an outside edit. The form shows it in the render that passes it, and your change does not make it dirty. - Computing a field from other fields in
onChangeand passing the result back asformData.
Other patterns have their own entries: storing an edit late, storing a rewritten copy or putting a record back by passing it again (A form passed formData shows each new value you pass), refusing an edit by not storing it (Refusing or rewriting an edit moves from onChange to transformChange), an onChange expected on mount (Form no longer calls onChange on mount for defaults or null fields) and calls to reset() (reset() goes back to the seed).
Verify
- No form is passed both
formDataandinitialFormData, and theform-data-both-seedswarning no longer appears. - The form shows the same data on mount as before.
Warns at run time as form-data-both-seeds.
Also read: