Created
July 16, 2021 04:21
-
-
Save Mulperi/cfa0f1209a5423d138927a152e1fff97 to your computer and use it in GitHub Desktop.
Make sure state is not updated after dismount
This file contains 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(() => { | |
let isMounted = true; // Used to make sure we don't make state update after component dismount. | |
const socket = io('ws://localhost:5000/live'); | |
socket.on('connect', function () { | |
console.log('CONNECTED'); | |
}); | |
socket.on('disconnect', function () { | |
console.log('DISCONNECTED'); | |
}); | |
socket.on('update', function (payload: any) { | |
fetch(`http://localhost:5000/images/${payload.src}`).then(() => { | |
if (isMounted) { | |
setLive(payload.src); | |
socket.emit('ready'); | |
} | |
}); | |
}); | |
/** On component destroy. */ | |
return () => { | |
socket.close(); | |
isMounted = false; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment