clean up react code example
Example 1: useeffect with cleanup
useEffect(() => {
//your code goes here
return () => {
//your cleanup code codes here
};
},[]);
Example 2: react useeffect
import React, { useEffect } from 'react';
const [data,setData]=useState()
useEffect(() => {
fetch(`put your url/api request in here`)
.then(res=>res.json())
.then(json => setData(json))
},[])
Example 3: 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);
};
});