Ricovitch
Ricovitch

Reputation: 2338

Simple way to update form values depending on changes with react-final-form

I'm trying to implement a form with two consecutive dependent Select with react-final-form and material-ui

Requirements

I struggle to find a simple and elegant solution to this requirements with react-final-form API.

There may be approaches with mutators, react-final-form-listeners, or decorators but this seems to me like a common use case of forms for which a simple solution without advanced APIs would be nice.


Here is a version with a combination of FormSpy component, useRef hook and the form.reset api.

https://codesandbox.io/embed/react-final-form-material-ui-field-dependencies-hn7ps


Here is another version using Declarating Form Rules approach via react-final-form-listeners like Erik adviced to do.

https://codesandbox.io/embed/react-final-form-material-ui-field-dependencies-ijxd0

It is a lot more simpler, as we dont need a react ref, and FormSpy.


Both solutions works almost perfectly except that the form.reset method redefines form initialValues under the hood, so that the RESET button do not reset form to the original initialValues anymore.

Is there a builtin way to hook in form state changes and compare old/new values ?

Upvotes: 9

Views: 12680

Answers (1)

Erik R.
Erik R.

Reputation: 7272

Perhaps Declarative Form Rules are what you need?

Upvotes: 4

Related Questions