question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

When used with react strict mode, the realtime database does not subscribe properly.

See original GitHub issue

Bug 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:

image

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:closed
  • Created a year ago
  • Reactions:2
  • Comments:17 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
ricardosikiccommented, Sep 15, 2022

I have removed <ReactStrictMode> and problem solved

1reaction
w3b6x9commented, Aug 23, 2022

if i pass config: { broadcast: { ack: true }} the default self: false disappears.

@pixtron that’s fine as the Realtime server will take the absence to mean self is false but i’ll go back and make the default config better. thanks for pointing this out.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found