Skip to content

Field name and value not in sync when changing field name #869

@capJavert

Description

@capJavert

What is the current behavior?

React final form does not switch values when field name is dynamically changed during component life, for example if we have:

const { input: { value } } = useField(name)

Imagine name is coming from props and can be dynamically changed.

Initial form state:
{ a: 1, b: 2 }

Renders:

  1. props { name: 'a' }, input: { value: 1 }
  2. props { name: 'b' } input: { value: 1 } - changed the field name, value is still stale
  3. props { name: 'b' } input: { value: 2 } - in next rerender name and value are in sync

So the case can exist where the source changed but the value is still from previous source.

This can break any component that expects specific value for specific source. And can also introduce wrong values (for example during auto save).

What is the expected behavior?

Renders:

  1. props { name: 'a' }, input: { value: 1 }
  2. props { name: 'b' } input: { value: 2 } - name and value are in sync in first render after name change
  3. props { name: 'b' } input: { value: 2 } - ...

Sandbox Link

https://codesandbox.io/s/final-form-field-name-and-value-not-in-sync-when-changing-field-name-okbtn

Click the button to change field name from a to b and watch console for output.

What's your environment?

"react": "16.13.1",
"react-final-form": "6.4.0",
"final-form": "4.19.1"

Any OS/Browser.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions