-
Notifications
You must be signed in to change notification settings - Fork 2.7k
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
useSubscription stop working in React v18 StrictMode #9664
Comments
I'm experiencing the same issue in strictmode. |
#7608 seems to be a related issue |
I have created a repository that reproduces this issue. I think it is related to restoring the state when StrictMode is enabled.
https://reactjs.org/blog/2022/03/29/react-v18.html#new-strict-mode-behaviors
If I have more time today, I will check the Apollo Client source code. |
Once a I don't understand why reusing an A commit for debugging: Removed StrictMode I remove StrictMode and manually remount the component, |
I understand why we should not reuse When apollo-client/src/core/QueryManager.ts Lines 985 to 996 in 9bcda65
apollo-client/src/utilities/observables/Concast.ts Lines 235 to 240 in 9bcda65
When React unmounts a component,
apollo-client/src/utilities/observables/Concast.ts Lines 141 to 147 in 9bcda65
Even if I think it is easier to recreate the I'm not familiar with zen-observable and Apollo Client code, so please let me know if there is something wrong 🙏 |
React18 automatically unmounts and remounts components in StrictMode. When React remount a component, the previous state is restored. However, if the previous state is reused, the subscription will not work. So we should recreate the necessary object when React remounts the component.
React18 automatically unmounts and remounts components in StrictMode. When React remount a component, the previous state is restored. However, if the previous state is reused, the subscription will not work. So we should recreate the necessary object when React remounts the component.
Fix useSubscription bug in React v18 <StrictMode> (#9664)
Copying my comment from #9707 (comment): I think apollo-client has an issue when re-mounting subscription components without the StrictMode now. This issue is reproducible on our open source desktop app ExpressLRS Configurator. We were able to fix subscription re-mount issues in development build by wrapping whole app inside <React.StrictMode> component. See ExpressLRS/ExpressLRS-Configurator#351. But nightly builds are still not working at /~https://github.com/ExpressLRS/ExpressLRS-Configurator-Nightlies. |
Intended outcome:
In React v18
StrictMode
,data
should be received fromuseSubscription()
.Actual outcome:
data
is alwaysundefined
even though the server actually sends it via websocket. Find more details in "How to reproduce the issue" below.How to reproduce the issue:
StrictMode
App
component,loading
will befalse
, anddata
will always beundefined
.{ type: "stop" }
message right after{ type: "start" }
If I remove
<React.StrictMode>
from the code, the app would work properly. We gotdata
from the server, and{ type: "stop" }
message would not be fired.Versions
Dependencies in
package.json
The text was updated successfully, but these errors were encountered: