socket.io-react-hook icon indicating copy to clipboard operation
socket.io-react-hook copied to clipboard

useSocketEvent with callback?

Open niekvenlo opened this issue 6 months ago • 1 comments

It looks like it would be fairly simple to add callback support to the useSocketEvent hook. The API would then support something like this:

useSocketEvent('name-of-event', (response) => handleResponse(response));

It could be as simple as adding this effect:

useEffect(() => callback(lastMessage), [lastMessage]);

(Probably not wise to add callback to deps, because callback will change on every render)

Is there a good reason not to include this functionality?

niekvenlo avatar Jul 10 '25 07:07 niekvenlo

useSocketEvent<string>(socket, "eventName", { onMessage });

There is a callback, onMessage

nitedani avatar Jul 10 '25 07:07 nitedani