iTranslated by AI
Why Reducing useEffect is Important in React Design
Introduction
Have you ever found yourself writing React code only to realize later that your component is filled with useEffect?
During the past six months working on React shared infrastructure and hook design, I have frequently heard the opinion that "fewer useEffects are better." However, I didn't fully grasp why this was the case.
In this article, I will organize my thoughts from the perspective of why a design with fewer useEffects is easier to handle, rather than making the reduction of useEffects an end in itself.
Is useEffect Evil?
To conclude, useEffect is not evil.
- Synchronizing with external data
- DOM manipulation
- Managing subscriptions
There are definitely processes that can only be expressed through useEffect.
The real issue occurs when:
You write logic in
useEffectthat could have been expressed through calculations of state or props instead.
I feel this is where the trouble begins.
Why useEffects Tend to Multiply
It is very natural to increase the number of useEffects when you are just starting to get used to React.
- I want to do something when a value changes.
- I want to process API results.
- I want to synchronize two different states.
For these requirements, the tendency is to:
Just watch it with
useEffectfor now.
This thought process is quite common.
What Makes Having Too Many useEffects Difficult?
As the number of useEffects grows, the following problems tend to occur:
- Managing dependency arrays becomes difficult.
- The timing of process execution is not intuitive.
- It is hard to track "why" a state changed.
- It is difficult to predict the scope of impact when making changes.
Especially in shared infrastructure and hooks, a design where:
It is unclear when and why a process runs
can significantly reduce maintainability.
Design Principles to Reduce useEffects
The essence of a design that reduces useEffects is:
Think about whether you can write it declaratively before trying to solve it with side effects.
I believe this is key.
1. Avoid Derived State
Values that can be calculated from props or state should not be held as state.
// ❌ Example of synchronizing with useEffect
const [total, setTotal] = useState(0);
useEffect(() => {
setTotal(price * quantity);
}, [price, quantity]);
// ✅ Calculate during render
const total = price * quantity;
2. Separate "Events" from "State Changes"
Clear events such as user operations should be processed in event handlers, not useEffect.
const handleSubmit = () => {
submitForm(formState);
};
3. Limit useEffect to "Boundaries with the Outside"
useEffect works best when limited to synchronizing with the outside of React, such as:
- API communication
- localStorage
- window / document
- subscriptions
Keeping it this way makes the design simpler.
When useEffect Is Necessary
- Data fetching on initial mount
- Starting and cleaning up subscriptions
- Synchronizing state with external APIs
Summary
- useEffect is not evil.
- However, having too many makes understanding and maintenance difficult.
- Before solving with side effects, consider whether it can be written declaratively.
- Keep useEffect confined to the "boundary with the outside."
Discussion