-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
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
Rendered fewer hooks than expected with React Fast Refresh while using observer #2668
Comments
Wow, this is really weird. Even pasting a single additional I would love to say it's some issue between fast refresh and/or code sandbox, but you are right it's not happening without an observer. This is some awkward edge case it seems. I have no explanation of what's going on here. Might be worth asking React team perhaps, I can't imagine how the can library cause anything like this. @Bnaya Do you possibly think it could be related to some of those memory cleanups? |
I had to re-read mobxjs/mobx-react-lite#226 for a reminder, but it seems that the proposed fix is still present in the code, so it's probably something else. |
I also tested on my react native 0.63.x project (that use react-table and mobx-react-lite latest) also trigger error rendered fewer hooks while add React.useEffect script like sample above And on my case, this error will occur while I use react-table with observer wrapper and add some react hooks (for trigger fast refresh) on the observed component. And here some screenshots, hope them will be useful for investigation. |
Firstly I would verify whether it's the same problem as before - mobx/packages/mobx-react-lite/src/utils/createReactionCleanupTrackingUsingFinalizationRegister.ts Line 21 in b3a993c
|
It occurs also before v3.1.0 |
TL; DR;
I've debugged the issue into React's code.
What goes wrong? The component that Means that the owning component of the hooks inside For a reason i'm still debugging, changing the hooks inside the function passed to Update 1: Update 2: |
Hm I hope the conclusion isn't that we are breaking the rules of hooks and intercepting functional component is simply not possible... |
I think it's a limitation/bug of react refresh, |
FWIW I run into this quite regularly in a project that doesn't even use MobX. I suspect it is HoC's that aren't handled nicely with fast-refresh. |
HOCs are fine, but you have to create an element, not to call the function directly. Such HOCs are impossible with hooks, unless you make some assumptions either about HOC (can't contain conditionals) or about wrapped component (doesn't contain hooks) |
I have few ideas for workarounds, such as making it look like there are directly custom hooks inside wrappedComponent (it worked for the non-mbox repro) |
React bug: facebook/react#20417 |
Closing here as an issue is filed at relevant packages. |
Something is dubious here. The issue does not occur in Create React App, so I don't think this was a core issue in Fast Refresh itself. I think there was a flaw in the CodeSandbox integration, which got fixed in codesandbox/codesandbox-client#5442. The React Native issue is worrying (#2668 (comment)). I would expect the React Native implementation to be correct. Can you make a standalone reproducing project I could clone? So I could look at this myself. |
When I comment out |
Wow. I have no idea why it didn't repro for me. I tried like 10 times. I'll reopen then. Thanks! |
Hmm. So the issue here is that MobX |
Yes (depends on definition I quess). We need to intercept the rendering itself, can we do something about it? |
I have a potential fix in facebook/react#21104. I verified it fixes the crash in the original report. facebook/react#20417 has a few more other reports of things not updating for MobX users though. Not sure if these are related, will need to take a closer look. |
|
i think i solved this problem inside react-refresh. I disabled the next update if the component name is "observerComponent". I don't know if I should do a PR as this is a mobx specific issue and maybe the react team doesn't want to include: misaeldossantos/react@f6eb19d. Maybe it's possible to add a property in the component function to disable refresh in any component that needs it: Component.disableReactRefresh = true, but I'm not sure that's a good thing |
Intended outcome:
No error occurs while update script and fast refresh triggered while using wrapped observer component
Actual outcome:
Rendered fewer hooks than expected occurs while using Fast Refresh and wrapped observer component
How to reproduce the issue:
I have created codesandbox : https://codesandbox.io/s/react-fast-refresh-test-forked-3ksts
to
And error will trigger like below screenshot
And redo it again without "observer" and no errors will occurs. I think this problem still same with old problem mobxjs/mobx-react-lite#226
Note : I use observer wrapper in purpose to reproduce this bugs and provide simple proof
Versions
mobx : 6.0.4 latest
mobx-react-lite : 3.1.6 latest
The text was updated successfully, but these errors were encountered: