In React, useEffect is a hook that runs side effects in your components — like API calls, DOM updates, timers, etc.
useEffect(() => {
console.log("Component mounted");
}, []);
Runs only once on mount if the dependency array is empty.
Can also watch specific values and re-run when they change.
It's like combining componentDidMount, componentDidUpdate, and componentWillUnmount in one place for functional components.
Always remember to cleanup timers or listeners:
useEffect(() => {
const timer = setInterval(() => console.log("Tick"), 1000);
return () => clearInterval(timer);
}, []);
React smartly manages your logic with useEffect, keeping your UI reactive and clean. 💡
Top comments (0)