How to subscribe/unsubscribe from WebSockets with @preact/signals-react

121 Views Asked by At

I've used React's useState and useEffect for quite some time. I'm just now experimenting with Signals using @preact/signals-react. Because state can be managed outside of React, I'm trying to create StatusSignal.ts that can be accessed from a variety of React components.

Status is collection of server values that will be updated via WebSockets. My current method of subscribing and unsubscribing from WebSockets is:

useEffect(() => {
  socket.on("connect", connection_handler);

  return () => {
    socket.off("connect", connection_handler);
  }
}, [] );

If I switch over to Signals, how do I accomplish this?

0

There are 0 best solutions below