3

I'm trying to stop a SignalR connection when a hook unmounts. The stop function is a Promise which needs to be resolved before the hook unmounts.

useEffect(() => {
  connection.start();

  return () => {
    connection.stop(); // Promise
  };
}, []);

But then I get this good ol' warning:

Warning: Can't perform a React state update on an unmounted component. This is a no-op...

I've tried extracting the stop functionality into an async function and calling that in unmount, but I get the same error. How can I stop the connection when the hook unmounts?

judehall
  • 884
  • 12
  • 27
  • How is state impacted by connection.stop()? What's the rest of your component look like? – Michael Landis Apr 04 '20 at 05:00
  • I am wondering if it wouldn't be better to have a one SignalR connection started and reused instead of starting it each time in useEffect. One of the reasons is that starting connection is actually quite expensive (in terms of network latency). Another reason might be you would not have to stop it hence you would not face the problem. – Pawel Apr 08 '20 at 05:13

0 Answers0