Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Do not re-render children on closed Collapsible #213

Open
jakub-zawislak opened this issue Oct 27, 2021 · 0 comments
Open

Do not re-render children on closed Collapsible #213

jakub-zawislak opened this issue Oct 27, 2021 · 0 comments

Comments

@jakub-zawislak
Copy link

I had a performance issue while using multiple Collapsibles, because the closed ones were re-rendering unnecessarily.

I have a form that consists multiple Collapsibles, like this:

<Formik>
  {({ values }) => (
    <Form>
      <Collapsible open={open == 'formPartA'}><FormPartA/></Collapsible>
      <Collapsible open={open == 'formPartB'}><FormPartB/></Collapsible>
      <Collapsible open={open == 'formPartC'}><FormPartC/></Collapsible>
      ...
    </Form>
  )}
</Formik>

When I change something in one part of my form, then values is changing and all of the FormPartX want to re-render, even if they are closed. It's a performance issue when the form is complex.

We need functionality that will block passing children when the Collapsible is closed.

I have fixed this by writing this code:

const Card = ({ children, isOpen, /* ... */ }) => {
  // ...

  const [isClosing, setIsClosing] = useState(false)

  useEffect(() => {
    if (isOpen) {
      setIsClosing(true)
    }
  }, [isOpen])

  return (
    <Collapsible
      open={isOpen}
      onClose={() => setIsClosing(false)}
      /* ... */
    >
      {(isOpen || isClosing) && children}
    </Collapsible>
  )
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant