A form passed formData shows each new value you pass
Detect
- A form passed
formDatawhoseonChangereturns without storing the event, to refuse an edit. - An
onChangethat stores a rewritten copy offormDatathat can undo the edit just made, such as a trimmed string. - A Cancel or Revert button that puts the record back by passing it as
formDataagain, while the form is still passed that record. - An
onChangethat stores an edit late, after anawaitor a server round trip, or that stores the server's response. - A
formDatathat isundefinedon some renders, such as arecordthat isundefineduntil it loads. In development, the console shows a warning ending in[form-data-ownership-changed].
Change
A form passed formData shows every edit as it is made, whether or not onChange stores it, as in v6. What it reads from the prop is news: a value other than the one you passed before. The value you passed before, or a copy with the same content, leaves the form as it is. Four kinds of v6 code show the difference:
- Code that refuses an edit by not storing it no longer puts the form back, where v6 did at the next prop change. Refuse it in
transformChange, see Refusing or rewriting an edit moves from onChange to transformChange. - Code that stores a rewritten copy of an edit has the copy shown, except where the rewrite undoes the edit, as trimming a space just typed does. That is the value you passed before, so the edit stays shown, and is submitted. Rewrite it in
transformChangeinstead. - Code that puts a record back by passing it again, as a Cancel button does with the record the form mounted with, changes nothing when that record is still the value you pass. Call
reset()on the form's ref, or remount the form under akey. - Code that stores an edit late keeps the edits made since when what it stores is the value
onChangereported, or a copy of it, a JSON copy included. The form remembers the latest 100 values it reported since you last passed its current data, and one of those changes nothing. A value that differs in anything, such as a server's response with anidor anupdatedAtadded, or a field it normalized, is a new value, which replaces the edits made since, as in v6. Until you have caught up, a value of your own that equals an earlier edit is taken for that edit.
Put a record back with reset():
<Form ref={formRef} schema={schema} validator={validator} formData={record} onSubmit={save} />
-<button onClick={() => setRecord({ ...record })}>Cancel</button>
+<button onClick={() => formRef.current?.reset()}>Cancel</button>
Store each edit as onChange reports it and save from that state, or pass initialFormData and let the form own the data:
-const onChange = async ({ formData }) => setData(await api.save(formData));
+const onChange = ({ formData }) => setData(formData);
+const onSubmit = ({ formData }) => api.save(formData);
A render that passes formData={undefined} leaves the form the data it has, and the form owns that data until a render passes formData again. In development the form warns once when it changes between the two, as React does for an input that changes between controlled and uncontrolled. The warning's ID is form-data-ownership-changed. v6 code such as formData={record} with a record that is undefined until it loads gets it. Render the form once the record has loaded:
-<Form schema={schema} validator={validator} formData={record} onChange={save} />
+{record && <Form schema={schema} validator={validator} formData={record} onChange={save} />}
Verify
- An edit your code refuses or rewrites shows as
transformChangeleaves it. - A Cancel button puts the record back.
- Edits made while a late store is pending stay shown once it lands, unless what it stores differs from the value
onChangereported. - The
form-data-ownership-changedwarning no longer appears.setWarningOptions()can make it throw.
Warns at run time as form-data-ownership-changed.
Also read: