In React, useEffect is the go-to hook for managing side effects—actions that happen outside the normal component rendering flow.

If you're fetching data, setting up a subscription, manually changing the DOM, or working with timers, you’re in useEffect territory.

✅ What is useEffect Used For?

React's rendering is pure, but sometimes you need to:

useEffect lets you run code after the DOM is updated.

🧠 Example

useEffect(() => {
    console.log("Component mounted!");
    return () => console.log("Component will unmount!");
}, []);

Explanation

🔄 When Does useEffect Run?

Dependency Array Behavior
[] Run once on mount
[someVar] Run on the mount, and when someVar changes
(no array) Run after every render

🚫 Common Mistakes with useEffect

🛠️ Pro Tip

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.