You might not need an Effect
Effects are escape hatches. They are not the default place for application logic.
A strong React engineer tries to keep logic in the simplest model that fits:
This chapter is about recognizing when an Effect is solving the wrong problem.
Derived data belongs in renderโ
Bad:
const [firstName, setFirstName] = useState('Aisha');
const [lastName, setLastName] = useState('Khan');
const [fullName, setFullName] = useState('');
useEffect(() => {
setFullName(`${firstName} ${lastName}`);
}, [firstName, lastName]);
Better:
const fullName = `${firstName} ${lastName}`;
Why?
The Effect version creates:
- extra state;
- an extra render;
- synchronization code;
- another place for bugs.
Filtering and sorting usually belong in renderโ
Bad:
const [visibleProducts, setVisibleProducts] = useState([]);
useEffect(() => {
setVisibleProducts(
products.filter(product => product.name.includes(query)),
);
}, [products, query]);
Better:
const visibleProducts = products.filter(product =>
product.name.includes(query),
);
If the computation later becomes measurably expensive, performance techniques can be considered. Do not introduce an Effect merely because a calculation is non-trivial.
User actions belong in event handlersโ
Bad:
const [submitted, setSubmitted] = useState(false);
useEffect(() => {
if (submitted) {
sendOrder(cart);
}
}, [submitted, cart]);
Better:
function handleSubmit() {
sendOrder(cart);
}
The order is sent because the user submitted the form, not because the component happened to render with submitted === true.
Avoid Effect chains for internal state transitionsโ
Bad:
useEffect(() => {
if (card !== null) {
setGoldCardCount(c => c + 1);
}
}, [card]);
useEffect(() => {
if (goldCardCount > 3) {
setRound(r => r + 1);
setGoldCardCount(0);
}
}, [goldCardCount]);
This turns a simple event into a chain of renders and Effects.
Prefer calculating the complete transition in the event that caused it:
function handlePlaceCard(nextCard) {
setCard(nextCard);
if (nextCard) {
setGoldCardCount(current => {
const next = current + 1;
if (next > 3) {
setRound(r => r + 1);
return 0;
}
return next;
});
}
}
For complex transitions, a reducer may model the state machine more clearly.
Resetting state when a key changesโ
Suppose a profile editor should reset when switching users.
A common Effect solution is:
function ProfilePage({userId}) {
const [comment, setComment] = useState('');
useEffect(() => {
setComment('');
}, [userId]);
}
But this is fundamentally an identity problem.
A clearer approach can be:
<ProfileEditor key={userId} userId={userId} />
Now React treats each user editor as distinct stateful identity.
Use identity when the state conceptually belongs to a different entity.
Adjusting state during render is usually a design smellโ
Sometimes code tries to keep state synchronized with props:
const [selection, setSelection] = useState(null);
useEffect(() => {
setSelection(null);
}, [items]);
First ask whether you can store a stable identifier instead:
const [selectedId, setSelectedId] = useState(null);
const selection = items.find(item => item.id === selectedId) ?? null;
Now the current selection is derived from current data.
Informing a parent about state changesโ
Bad:
function Toggle({onChange}) {
const [enabled, setEnabled] = useState(false);
useEffect(() => {
onChange(enabled);
}, [enabled, onChange]);
}
This causes the notification after rendering.
If the change comes from user interaction, notify both places in the same event:
function Toggle({onChange}) {
const [enabled, setEnabled] = useState(false);
function handleClick() {
const next = !enabled;
setEnabled(next);
onChange(next);
}
}
Or make the component controlled if the parent should own the source of truth.
Parent and child state duplicationโ
If both parent and child need the same changing value, do not synchronize copies with Effects.
Prefer one source of truth:
This is the controlled-component model.
Initializing application stateโ
Sometimes developers use an empty-array Effect to initialize state:
useEffect(() => {
setTheme(loadTheme());
}, []);
If the value can be determined during state initialization, prefer a lazy initializer:
const [theme, setTheme] = useState(() => loadTheme());
This avoids a render with temporary state followed by an Effect-driven correction.
Expensive calculations are not Effectsโ
Wrong mental model:
"This computation is expensive, so I should run it in an Effect."
Effects are about synchronization, not computation cost.
Start with:
const result = expensiveCalculation(data);
Then measure. If memoization is justified later:
const result = useMemo(() => expensiveCalculation(data), [data]);
Even then, the computation remains part of rendering logic.
Network requests caused by eventsโ
Submitting a purchase:
function handleBuy() {
fetch('/api/order', {
method: 'POST',
body: JSON.stringify(cart),
});
}
This belongs in the event because the user caused it.
Fetching data because a route/component needs to stay synchronized with a remote resource may use an Effect in a simple client-only app, but larger applications often benefit from framework loaders or server-state libraries.
Analytics: event or Effect?โ
Question:
Why is the analytics event happening?
If it means "user clicked checkout":
function handleCheckout() {
analytics.track('checkout_clicked');
}
If it means "this screen became active/visible": an Effect may be appropriate because the analytics is tied to rendered presence.
The important distinction is cause.
Common anti-pattern: Effect as command busโ
Bad:
const [action, setAction] = useState(null);
useEffect(() => {
if (action === 'SAVE') save();
if (action === 'DELETE') remove();
}, [action]);
This converts direct imperative events into indirect state-driven commands.
Prefer calling the command from the event that caused it.
Decision tableโ
| Problem | Usually use |
|---|---|
| Calculate a value for JSX | render logic |
| Respond to click/submit/input | event handler |
| Share state between siblings | lift state |
| Reset state for a different entity | key / identity |
| Model complex state transitions | reducer |
| Store mutable non-render data | ref |
| Synchronize browser/network/widget/subscription | Effect |
Debugging unnecessary Effectsโ
When reviewing an Effect, ask:
- What external system is this synchronizing with?
- If there is none, can the value be derived during render?
- Did a specific user interaction cause this work?
- Is duplicated state being synchronized?
- Is this really a component identity problem?
- Would lifting state remove the need for synchronization?
- Would a reducer better model a state transition?
Production exampleโ
Imagine an ecommerce filter page.
Avoid:
Prefer:
One source of truth produces multiple derived values.
Exerciseโ
Refactor this component without Effects:
function Checkout({items, onTotalChange}) {
const [total, setTotal] = useState(0);
useEffect(() => {
setTotal(items.reduce((sum, item) => sum + item.price, 0));
}, [items]);
useEffect(() => {
onTotalChange(total);
}, [total, onTotalChange]);
return <strong>{total}</strong>;
}
Questions:
- Which value is derived?
- Who should own the total?
- Does the parent really need a callback, or can it calculate the same value?
Interview questionsโ
Junior: Give an example of logic that should be calculated during render instead of inside an Effect.
Mid-level: Why can Effect-driven derived state cause extra renders and synchronization bugs?
Senior: How do state ownership and component identity eliminate classes of Effects from an application architecture?
Summaryโ
Effects are valuable precisely because they are specialized.
Referencesโ
- https://react.dev/learn/you-might-not-need-an-effect
- https://react.dev/learn/sharing-state-between-components
- https://react.dev/learn/preserving-and-resetting-state
Nextโ
Continue with Effect Lifecycle and Dependencies.