In React, useEffect is the go-to hook for managing side effects—actions that happen outside the normal component rendering flow.
useEffect
If you're fetching data, setting up a subscription, manually changing the DOM, or working with timers, you’re in useEffect territory.
React's rendering is pure, but sometimes you need to:
setInterval
setTimeout
localStorage
useEffect lets you run code after the DOM is updated.
useEffect(() => { console.log("Component mounted!"); return () => console.log("Component will unmount!"); }, []);
Explanation
[]
componentDidMount
componentWillUnmount
[someVar]
someVar
Use the React DevTools to debug hooks and watch when your effects fire. You can also create custom hooks (e.g. useFetch) to abstract effect logic.
useFetch