japgolly / scalajs-react

Facebook's React on Scala.JS
https://japgolly.github.io/scalajs-react/
Apache License 2.0
1.64k stars 231 forks source link

Functional components that reference Reusability throw "Too many re-renders" is there's a Reusability.never #1027

Closed rpiaggio closed 2 years ago

rpiaggio commented 2 years ago

I'm not sure if this is a bug, but it surely took us some time to figure out.

Since Reusability is implemented for functional components by using a counter in a useState, having a Reusability.never will cause infinite re-renders if renderWithReuse or withReuse are used to render. I suspect this will also be the case in use*WithDeps hooks.

Basically, this line will be invoked in every render: https://github.com/japgolly/scalajs-react/blob/master/coreGeneric/src/main/scala/japgolly/scalajs/react/hooks/CustomHook.scala#L187.

This is somewhat different behavior than when using Reusability.never on a class component, where if all dependencies have Reusability.never then the component will behave the same way as if reusability was not set.

japgolly commented 2 years ago

Hey @rpiaggio, thanks for raising and tracking down the cause! Fixed. I'll get a release out tomorrow

rpiaggio commented 2 years ago

Thank you!