Skip to content

Instantly share code, notes, and snippets.

@amcsi
Created July 2, 2024 09:08
Show Gist options
  • Save amcsi/9a0e8a567d738169348757dadc4f032b to your computer and use it in GitHub Desktop.
Save amcsi/9a0e8a567d738169348757dadc4f032b to your computer and use it in GitHub Desktop.
function useWebsocketWithCallback(callback, ...args) {
const {lastMessage} = useWebsocket(...args);
const lastMessageRef = useRef();
lastMessageRef.current = lastMessage;
useEffect(() => {
if (lastMessage !== lastMessageRef.current) {
callback(lastMessage);
}
}, [lastMessage]);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment