kaliberjs / forms

Forms for React
MIT License
4 stars 0 forks source link

Cannot use props from outside render function scope, due to useMemo #7

Closed peeke closed 4 years ago

peeke commented 4 years ago

I'm running into problems using the FormFieldValid component with the following setup:

function NewsletterForm({ form, formSubmitting, onSubmit }) {
  const { fields } = form

  return (
    <form {...{ onSubmit }}>
      <Fields {...{ fields }} />
      <FormFieldValid field={form} render={valid => (
        <Button 
          type="submit" 
          disabled={formSubmitting || !valid} 
          loading={formSubmitting}
        >
          Meld je aan
        </Button>
      )} />
    </form>
  )
}

In the code changes in formSubmitting are never picked up, because the render result is memoized based on the validity.

All fixes I could think of have some kind of drawback:

@EECOLOR What do you think?

EECOLOR commented 4 years ago

Removing the useMemo is what you want. If it was any other component it would look like this:

<Other ...
   <Button ...
     ...

So it would be rendered the same amount of times. I think the performance impact is minimal (if it's even there).

My advise: take the simple route, remove the useMemo and start the hard thoughts once you encounter a real problem.