Last active
May 18, 2022 15:07
-
-
Save ljaviertovar/9f1bbf59f980512d77df73115a0a4931 to your computer and use it in GitHub Desktop.
Cleaning up Web Sockets functions in React
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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