Skip to content

Instantly share code, notes, and snippets.

@ljaviertovar
Last active May 18, 2022 15:07
Show Gist options
  • Save ljaviertovar/9f1bbf59f980512d77df73115a0a4931 to your computer and use it in GitHub Desktop.
Save ljaviertovar/9f1bbf59f980512d77df73115a0a4931 to your computer and use it in GitHub Desktop.
Cleaning up Web Sockets functions in React
useEffect(() => {
const ws = new WebSocket(url, protocols)
// do what you want with the socket
ws.onmessage = (event) => {
setValue(JSON.parse(event.data));
};
// cleanup the web socket when component unmout
return () => ws.close()
}, [])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment