When updating HOC-wrapped component to a different number//kinds of hooks, you get the "Rendered fewer hooks than expected" error from React
Repro:
https://codesandbox.io/s/react-refresh-webpack-plugin-rendered-more-hooks-than-during-the-previous-render-issue-ezcrz?file=/src/Comp.js
Delete or add one of the hooks there, and you should get the error
Background:
at mobx we've got a bug report, and we've figured it's not on mobx side, but react-refresh/the webpack plugin
mobxjs/mobx#2668, as in the above repro.
After some debugging, It seems like when HOC, renderprops or similar composition patterns are involved,
The the relevant components aren't getting signature hash,
And React, when the old and new component does not have a signature, tries to refresh the component anyhow:
https://github.com/facebook/react/blob/9aca239f11f31109dc1a229aa1571c2bf02f5524/packages/react-refresh/src/ReactFreshRuntime.js#L126-L132
I've opened the issue here because injecting the hash for a component in a co-op between the plugin and react-refresh.
I've going to open the issue there as-well
Keywords:
Rendered more hooks than during the previous render
Rendered fewer hooks than expected