Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

sample form

Requirements

  • Both fields are required
  • When user selects a country value
    • city field should reset to blank
    • city field should not be marked as invalid
  • User should be able to reset the form completely anytime

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

  • subscribre to form values via FormSpy onChange prop
  • update a react ref each time values change
  • compare ref values with new values and use form.reset API to reset city field value without triggering validation

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 ?

like image 541
Ricovitch Avatar asked Jun 21 '19 09:06

Ricovitch


People also ask

How do you get form values in react final form?

You can use the form values from the render props of final-form. import { useFormState } from 'react-final-form'; const ChildComponentOfForm = () => { const { values } = useFormState(); const value = values['field-name']; ... }

Does react final form use Redux?

Good news! React Final Form was written by the same guy (@erikras) that wrote Redux Form, so much of the API is exactly the same.

What is pristine in react final form?

The variable pristine sets the disable prop to true only when the form loads for first time, but when the user submit the form (not reloading the page) the variable pristine is false and I expect it to be true in order to disable the "Submit" button. The Submit button is disabled when the form loads for first time.

How does react final form work?

React Final Form is a thin React wrapper for Final Form, which is a subscriptions-based form state management library that uses the Observer pattern, so only the components that need updating are re-rendered as the form's state changes.


1 Answers

Perhaps Declarative Form Rules are what you need?

like image 116
Erik R. Avatar answered Sep 28 '22 05:09

Erik R.