final-form / react-final-form

🏁 High performance subscription-based form state management for React
https://final-form.org/react
MIT License
7.38k stars 479 forks source link

Form values reset on render with deep initialValues #731

Open antoniomadonna opened 4 years ago

antoniomadonna commented 4 years ago

Are you submitting a bug report or a feature request?

bug report

What is the current behavior?

When the form has deep (with nested properties) initialValues and re-renders it will reset all the form state.

What is the expected behavior?

It should not reset entered values

Sandbox Link

https://codesandbox.io/s/react-final-form-nested-values-bug-sn0mc

What's your environment?

"react-final-form": "^6.3.5", "final-form": "^4.18.7",

Other information

simstefani commented 4 years ago

You can make your deep equal, for example with lodash.isequal in initialValuesEqual. But yes it's a bug

harrycollin commented 4 years ago

I too have had this happen when using an object as the value with nested values.

veeralpatel commented 3 years ago

Any updates here?

pstrh commented 1 year ago

Thanks @simstefani for making us aware of initialValuesEqual.

An alternative solution to the deep equal (depending on the use case) would be to set initialValuesEqual={() => true}. The form can then be reset with a key prop or via formApi.restart(initialValues?: InitialFormValues) if needed.