final-form / react-final-form

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

How to get only changed fields? #396

Open juicylevel opened 5 years ago

juicylevel commented 5 years ago

feature request

Hello! I use FormSpy to handle form field changes. I need to know which fields have changed. How can i do this?

P.S. dirtyFields do not fit

iskandarovBakshi commented 5 years ago

you can use deep-object-diff package to know which fields changed, diff(initialValues, values)

elissaioskon commented 4 years ago

I was also searching about how i could implement this.

I post the solution that i used for any next visitor: Second param that is passed at onSubmit function https://final-form.org/docs/react-final-form/types/FormProps#onsubmit is named form.

You can use form.getState().initialValues and compare them with the first param of onSubmit which is values. The comparison could happen using an external library like deep-object-diff or a custom diff function.

steventilator commented 4 years ago

@juicylevel I don't know what you mean with "dirtyFields do not fit", but for everyone else: form.getState().dirtyFields gives you the fields that have changed.

Basovs commented 1 year ago

@juicylevel I don't know what you mean with "dirtyFields do not fit", but for everyone else: form.getState().dirtyFields gives you the fields that have changed.

"dirtyFields" doesnt work in multistep forms where each step mount and unmounts, "dirtyFields" resets/forgets step 1 dirtyFields when for example going from step 1 to step 2.