How do you clean up side effects in useEffect?
To clean up, return a function from the effect callback. This cleanup function is called when the component unmounts or before the effect re-runs.
For example: When using setInterval we need to clear it or else if will keep running even after the component is unmount
useEffect(() => {
const timer = setInterval(() => {
console.log('Timer running');
}, 1000);
return () => clearInterval(timer); // Cleanup
}, []);