Skip to content

useField v7.0.1 overwrites form values set via change() for previously unregistered field paths #1095

Open

Description

@StevenHensgen

Bug Report

Version

  • react-final-form: 7.0.1
  • final-form: 5.0.1
  • react: 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

  1. Step 1 component registers useField("parent") and calls form.change("parent.child.value", data) to set a nested value
  2. User advances to step 2 — step 1 unmounts, step 2 mounts
  3. Step 2 component registers useField("parent.child.value") to read the value
  4. Expected: useField returns data (the value set in step 1)
  5. Actual: useField overwrites it with initialValues.parent.child.value, discarding data

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

  1. christianaurichzm commented on Aug 13, 2026

    @christianaurichzm

    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 on form.destroyOnUnregister alone breaks #1085, because that same block is also what re-applies a changed initialValue prop. Details in the PR.

  2. PhilMeyr commented on Aug 20, 2026

    @PhilMeyr

    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's name prop changes from items[i] to items[i+1] (or vice versa) while the component itself survives.

    useField has name in its registration effect's deps. React runs all cleanup functions before any setup function on a re-render, and final-form does delete state.fields[name] on the last unsubscribe from a field — even without destroyOnUnregister. So by the time the moved instance's effect re-runs with its new name, form.getFieldState(name) returns undefined, and the "FIX #988" branch in registerField treats it as a fresh mount, calling form.change(name, getIn(initialValues, name)).

    The result isn't just a display glitch: the row at the new index is overwritten with whatever initialValues holds at that index — i.e. the value that used to belong to the row now one position over. It's a real form.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 destroyOnUnregister restoration still works). Happy to share a minimal repro if that's useful for moving #1096 forward.

  3. christianaurichzm commented on Aug 21, 2026

    @christianaurichzm

    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 row a edited 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 if fields["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 new name; it also only asserts that an undefined slot stays undefined. 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 destroyOnUnregister on, the array-shift case is not fixed by #1096, and behaves identically on main. 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. Plain destroyOnUnregister restoration (#1031) is unaffected and covered by tests.

    No repro needed, the tests cover it. Thanks for the detailed writeup.

  4. christianaurichzm commented on Aug 21, 2026

    @christianaurichzm

    @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 wrong initialValues entry after an insert or a remove. Details are in the thread above and in the PR.

    The PR shows no check because ci.yml is on: [push], so pull requests from forks never run it upstream. Green run on my fork: https://github.com/christianaurichzm/react-final-form/actions/runs/32513971954

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions