What 'Deriving State' Means
You'll learn: What derived state is and why computing values inside useEffect is an antipattern.
The Core Idea
"Deriving state" means calculating a value directly from existing props or state during render — not storing it in separate state. It's the opposite of the common mistake where developers compute something inside useEffect and then call setState to store the result.
Think of it like making a sandwich: you don't need to store the fact that your sandwich exists separately from storing the bread and filling. You can just look at the ingredients and say "yep, that's a sandwich." Derived state works the same way.
The Antipattern: Computing in useEffect
// ❌ Antipattern: Storing derived state
function UserGreeting({ firstName, lastName }) {
const [fullName, setFullName] = useState('');
useEffect(() => {
setFullName(`${firstName} ${lastName}`);
}, [firstName, lastName]);
return <h1>Hello, {fullName}!</h1>;
}
This creates unnecessary complexity: extra state, extra re-renders, and synchronization headaches.
The Right Way: Calculate During Render
// ✅ Correct: Derive the value
function UserGreeting({ firstName, lastName }) {
const fullName = `${firstName} ${lastName}`;
return <h1>Hello, {fullName}!</h1>;
}
The value is computed fresh every render from the source data. No useEffect, no extra state, no synchronization bugs.
Key Takeaway: If you can calculate something from props or state, don't store it in separate state inside useEffect — just derive it during render. Effects are for side effects, not data transformation.