kaliberjs / forms

Forms for React
MIT License
4 stars 0 forks source link

Hook into changed form state #11

Closed peeke closed 4 years ago

peeke commented 4 years ago

Say I want to do something effectful with the formState, every time it changes (e.g. store the values in localStorage, so the form can be restored on a reload). What would be the best way to do this?

The initial thought may be to use useFormFieldValue(form), but this will of-course re-render the whole form onChange (which is what this library explicitly wants to prevent.

Right now, we made a separate component, which uses this hook and calls a callback prop whenever the value changes. Maybe it would make sense to add an onChange property to the useForm config?

Example:

const { form: { fields }, submit } = useForm({
    initialValues: {}
    fields: {},
    // handle form change
    onChange: handleChange,
    // handle form submit
    onSubmit: handleSubmit,
  })
EECOLOR commented 4 years ago

Although it's not part of the 'official' API I think you can do fieldOrForm.value.subscribe(...).

If that works we can document it and with that make it official.

EECOLOR commented 4 years ago

snapshot is will be part of the API (see #15) So then you can use snapshot.subscribe(form, f)