kaliberjs / forms

Forms for React
MIT License
4 stars 0 forks source link

Perfomance inprovement for validity #19

Open EECOLOR opened 3 years ago

EECOLOR commented 3 years ago

https://github.com/kaliberjs/forms/blob/b937d8769e9d8500a38f4b1a418d99d9a1eb40e7/src/components.js#L17

FormFieldValid is now implemented with useSnapshot which means it will render on keystrokes. We could add another hook that prevents these unneeded renders, something like this:

function useFormFieldInvalid(field) {
  const [formFieldInvalid, setFormFieldInvalid] = React.useState(() => snapshot.get(field).invalid)

  React.useEffect(
    () => {
      setFormFieldInvalid(snapshot.get(field).invalid)
      return snapshot.subscribe(field, x => setFormFieldInvalid(x.invalid))
    },
    [field]
  )

  return formFieldInvalid
}