cleanup side effects react code example
Example 1: useeffect with cleanup
useEffect(() => {
//your code goes here
return () => {
//your cleanup code codes here
};
},[]);
Example 2: clean up useeffect
useEffect(() => {
function handleStatusChange(status) {
setIsOnline(status.isOnline);
}
ChatAPI.subscribeToFriendStatus(props.friend.id, handleStatusChange);
// Specify how to clean up after this effect:
return () => {
ChatAPI.unsubscribeFromFriendStatus(props.friend.id, handleStatusChange);
};
});