Repository navigation
Description
Bug Report
Version
react-final-form: 7.0.1final-form: 5.0.1react: 19.0.6
Description
The StrictMode fix added in v7.0.1 (PR #1068/#1069, fixing #1031) introduced a regression where useField overwrites existing form values when a field is registered at a path that was never previously registered as its own field — even when destroyOnUnregister is false (the default).
This breaks multi-step/wizard forms where different steps register useField at different paths within the same form state tree.
Reproduction
- Step 1 component registers
useField("parent")and callsform.change("parent.child.value", data)to set a nested value - User advances to step 2 — step 1 unmounts, step 2 mounts
- Step 2 component registers
useField("parent.child.value")to read the value - Expected:
useFieldreturnsdata(the value set in step 1) - Actual:
useFieldoverwrites it withinitialValues.parent.child.value, discardingdata
Root Cause
The fix added in v7.0.1 prepends this logic to useField's useEffect:
var existingFieldState = form.getFieldState(name);
if (!existingFieldState) {
var formInitialValue = getIn(formState.initialValues, name);
var valueToSet = formInitialValue !== undefined ? formInitialValue : initialValue;
if (valueToSet !== undefined) {
form.change(name, valueToSet);
}
}
The problem is that getFieldState(name) checks whether a field is registered at that exact path, not whether a value exists in the form state at that path. When a value was set via change() through a parent field (or directly on the form), no field registration exists at the nested path, so getFieldState returns null, and the code overwrites the existing value with initialValues.
This code was not present in v7.0.0 — in that version, useEffect simply called register() without the getFieldState check.
Suggested Fix
Before calling form.change(), check whether the form's current values already differ from initialValues at that path:
if (!existingFieldState) {
var formState = form.getState();
var currentValue = getIn(formState.values, name);
var formInitialValue = formState.initialValues ? getIn(formState.initialValues, name) : undefined;
var valueToSet = formInitialValue !== undefined ? formInitialValue : initialValue;
// Only reset if no value has been set via change()
if (valueToSet !== undefined && currentValue === undefined) {
form.change(name, valueToSet);
}
}
Workaround
Set the initial value for affected fields to undefined instead of null in initialValues. This causes valueToSet to be undefined, which skips the form.change() call entirely:
// Before (broken): initialValues triggers overwrite
{ Data: null }
// After (workaround): undefined skips the form.change() call
{ Data: undefined }
Activity
christianaurichzm commentedon Aug 13, 2026
Ran into this on an upgrade. Opened #1096 with a fix and regression tests.
The diagnosis above is right, and it got me most of the way. Two things I found while turning it into a patch, in case anyone is working around this locally: the suggested guard still overwrites after an intentional
change(name, undefined)and restores stale entries in array fields after an index shift; and gating onform.destroyOnUnregisteralone breaks #1085, because that same block is also what re-applies a changedinitialValueprop. Details in the PR.PhilMeyr commentedon Aug 20, 2026
We hit this bug independently in a slightly different shape than the wizard scenario described above, which might be useful context for #1096.
Our case: an editable list rendered with positional field names (
items[${index}].label) but stable React keys per row (e.g. an item id, not the index). Inserting a row at the head, or removing a row from the middle, moves existing row instances instead of unmounting/remounting them — so an instance'snameprop changes fromitems[i]toitems[i+1](or vice versa) while the component itself survives.useFieldhasnamein its registration effect's deps. React runs all cleanup functions before any setup function on a re-render, andfinal-formdoesdelete state.fields[name]on the last unsubscribe from a field — even withoutdestroyOnUnregister. So by the time the moved instance's effect re-runs with its newname,form.getFieldState(name)returnsundefined, and the "FIX #988" branch inregisterFieldtreats it as a fresh mount, callingform.change(name, getIn(initialValues, name)).The result isn't just a display glitch: the row at the new index is overwritten with whatever
initialValuesholds at that index — i.e. the value that used to belong to the row now one position over. It's a realform.change, so saving the form persists the wrong data, and any in-progress edit on a shifted row is lost.We backported #1096's diff as a local patch and confirmed it fixes this case too (verified with a regression test covering both insert-at-head and remove-from-middle, plus a check that legitimate
destroyOnUnregisterrestoration still works). Happy to share a minimal repro if that's useful for moving #1096 forward.christianaurichzm commentedon Aug 21, 2026
Thanks, I reproduced this independently and it checks out.
On
main, with stable keys and positional names, inserting at the head shifts every label one row over and drops the edit:[a:"A", b:"B", c:"C"]with rowaedited becomes[d:"A", a:"B", b:"C", c:"C"]. Removing from the middle does the same. #1096's guard fixes both.The failure also pins down the ordering you describe: the newly mounted head row gets reseeded from
initialValues[0], which can only happen iffields["items[0].label"]was already gone when it registered. So the moved row's cleanup ran before the new row's setup.Your case was not covered. The existing array test uses
key={i}, which ties React identity to position, so no surviving instance ever sees a newname; it also only asserts that anundefinedslot staysundefined. Yours overwrites a defined, user-edited value with a different one. I added both directions as regression tests on the PR.One correction worth recording: with
destroyOnUnregisteron, the array-shift case is not fixed by #1096, and behaves identically onmain. I measured both. Final Form deletes the value at unregister in that mode, so the "was this destroyed?" check is genuinely true, and nothing in the form state at registration time separates a rename from an unmount. Telling them apart means carrying the value across cleanup, which is the separate lifecycle change already deferred out of #1096. PlaindestroyOnUnregisterrestoration (#1031) is unaffected and covered by tests.No repro needed, the tests cover it. Thanks for the detailed writeup.
christianaurichzm commentedon Aug 21, 2026
@erikras would you consider #1096 for merge and a 7.0.2? It fixes a regression in the currently published v7.0.1. Three of us have reported it independently now, in two distinct shapes: values written through
change()at a path that has no registered field, and positional field names in an editable list, where rows get overwritten with the wronginitialValuesentry after an insert or a remove. Details are in the thread above and in the PR.The PR shows no check because
ci.ymlison: [push], so pull requests from forks never run it upstream. Green run on my fork: https://github.com/christianaurichzm/react-final-form/actions/runs/32513971954EPBDS-16211: four tests react-final-form 7.0.1 fails, and why the for…