reset() goes back to the seed it was last given
Detect
.reset(called on aFormref in your code, with or without an argument.resetpassed as an event handler, such asonClick={formRef.current.reset}, in your code.- A form that passes
formData={data}and stores every edit indatathroughonChange, and also callsreset().
Change
reset() with no argument works as in v6 until you first pass it a seed. It reads the seed from the current props (formData if you pass it, otherwise initialFormData) and fills it in with the defaults. It clears the submit count and every error except extraErrors, and reports the result through onChange.
reset(saved) makes saved the seed instead, for this and every later reset(). Those later calls go back to saved rather than the props' seed, even after the props pass another one. To read the props' seed again, remount the form under a key (see Making the saved data the baseline). A reset passed as an event handler now receives the event as its seed: see reset passed as an event handler.
Mind which seed you pass. If you pass formData={data} and store every edit in data, the seed is the latest edit, so reset() clears the errors and leaves the data as it is. To reset to the original record, pass the record as initialFormData (see Form takes its data as initialFormData or formData) and keep your copy of the edits in a separate state:
-<Form formData={data} onChange={(e) => setData(e.formData)} ref={formRef} />
+<Form initialFormData={record} onChange={(e) => setDraft(e.formData)} ref={formRef} />
Or remount the form under a key.
Verify
reset()with no argument shows the props' seed filled in with defaults, with the submit count and errors cleared andextraErrorskept.- After
reset(saved), a laterreset()returns tosaved, and a remount under a newkeyreturns to the props' seed. - A form that stores every edit resets to the original record once the record is passed as
initialFormData.
Also read: