When used with react strict mode, the realtime database does not subscribe properly.
See original GitHub issueBug report
Describe the bug
When we develop with the React strict mode, we can’t build subscriptions correctly.
To Reproduce
Turn on strict mode:
ReactDOM.createRoot(document.getElementById("root") as HTMLElement).render(
<React.StrictMode>
<App />
</React.StrictMode>
);
Using subscriptions in components:
function App() {
useEffect(() => {
const subs = supabase
.from("objects")
.on("UPDATE", (payload) => {
dispatch(updateGameObject(payload.new.data));
})
.subscribe(console.log);
subs.onClose(() => {
console.log("on closed");
supabase.removeAllSubscriptions();
});
return () => {
subs.unsubscribe();
};
}, []);
return <div />
}
Expected behavior
In strict mode, useEffect is triggered twice. In theory, it should quickly establish and close a connection once, and then establish it again.
However, in practice, it is not possible to subscribe properly. In strict mode, subscribe()
will receive the CLOSED signal directly at the first time.
Screenshots
The output in the console is as follows:
System information
- OS: Windows
- Browser (if applies): Edge
- Version of supabase-js: ^1.35.4
- Version of Node.js: 16
- Version of react: 18
Additional context
In supabase/supabase#7771 it is suggested that turning on strict mode can cause problems.
Issue Analytics
- State:
- Created a year ago
- Reactions:2
- Comments:17 (5 by maintainers)
Top Results From Across the Web
When used with react strict mode, the realtime database does ...
In strict mode, subscribe() will receive the CLOSED signal directly at the first time.
Read more >Strict Mode - React
StrictMode is a tool for highlighting potential problems in an application. Like Fragment , StrictMode does not render any visible UI.
Read more >firebase.database() is not a function error in react
StrictMode >, document.getElementById("root") );. store.js is just using redux toolkit to create and export a store.
Read more >Modern API data-fetching methods in React - LogRocket Blog
In the code, we are using the fetch() method to request post data from the resource endpoint as seen in the useEffect Hook....
Read more >React Notes - CS Courses With Web Pages Server
React implements a special syntax called JSX to let you write HTML directly in JavaScript. The root component is normally named App. It...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
I have removed
<ReactStrictMode>
and problem solved@pixtron that’s fine as the Realtime server will take the absence to mean
self
isfalse
but i’ll go back and make the default config better. thanks for pointing this out.