Why Effects are Often Overused
You'll learn why beginners (and sometimes experienced developers!) reach for useEffect too quickly, when simpler solutions exist.
The Problem: Effect Overload
Many developers treat useEffect like a Swiss Army knife — a tool for everything that needs to happen in a component. This leads to components bloated with effects that don't actually need to synchronize with external systems.
Think of it like using a chainsaw to slice bread. Sure, it works, but a knife is simpler, safer, and more appropriate.
What Belongs Elsewhere
Derived state doesn't need effects. If you can calculate a value from existing props or state during render, just do it:
// ❌ Overusing effect
const [items, setItems] = useState([]);
const [count, setCount] = useState(0);
useEffect(() => {
setCount(items.length);
}, [items]);
// ✅ Just calculate it
const count = items.length;
Event handlers are for responding to user actions. Don't use effects to do something that should happen because the user clicked:
// ❌ Effect for user interaction
useEffect(() => {
if (shouldSubmit) {
submitForm();
}
}, [shouldSubmit]);
// ✅ Event handler
const handleClick = () => {
submitForm();
};
Why This Happens
Effects run after render, making them feel like a "catch-all" place for logic. But this creates unnecessary render cycles and harder-to-follow code. The component renders, React commits to the DOM, then your effect runs and potentially triggers another update.
Key Takeaway: Effects are for synchronization with external systems (APIs, browser APIs, subscriptions). If your logic doesn't leave React's world, you probably don't need an effect. Calculate during render or respond in event handlers instead.