What is Derived State?
Derived state refers to state that is computed from props or other state rather than being the single source of truth itself. Instead of storing raw data, the component derives its value dynamically.
In simple terms: if a value can be calculated, it usually should not be stored.
Why Derived State Exists
Derived state is mainly used to:
- Transform incoming props
- Sync state with prop changes
- Cache expensive computations (rare cases)
For example, filtering or formatting data before displaying it in tools like dynamic form builders is a common derived-state use case.
Basic Example of Derived State (Anti-Pattern)
function UserProfile({ user }) {
const [name, setName] = useState(user.name);
useEffect(() => {
setName(user.name);
}, [user.name]);
return <p>{name}</p>;
}
⚠️ This is usually an anti-pattern because:
- State is duplicated
- Extra re-renders occur
- Risk of data going out of sync
Correct Way: Compute Instead of Store
function UserProfile({ user }) {
const name = user.name;
return <p>{name}</p>;
}
This keeps a single source of truth and avoids unnecessary state.
Derived State Using Computation
Example: Filtering Data
function ProductList({ products, search }) {
const filteredProducts = products.filter(product =>
product.name.toLowerCase().includes(search.toLowerCase())
);
return (
<ul>
{filteredProducts.map(p => (
<li key={p.id}>{p.name}</li>
))}
</ul>
);
}
Here, filteredProducts is derived state but not stored in React state.
Derived State with useMemo (Performance Optimization)
When the computation is expensive, use useMemo.
import { useMemo } from "react";
function ProductList({ products, search }) {
const filteredProducts = useMemo(() => {
return products.filter(product =>
product.name.toLowerCase().includes(search.toLowerCase())
);
}, [products, search]);
return (
<ul>
{filteredProducts.map(p => (
<li key={p.id}>{p.name}</li>
))}
</ul>
);
}
This is commonly used in real-world applications that render large datasets, such as previewing content layouts while generating customized documents.
When Derived State is Actually Needed
Derived state is acceptable when:
- You need to reset internal state when a prop changes
- You need to track previous prop values
- You must control animations or transitions
Example: Resetting State on Prop Change
function Form({ initialEmail }) {
const [email, setEmail] = useState(initialEmail);
useEffect(() => {
setEmail(initialEmail);
}, [initialEmail]);
return (
<input
value={email}
onChange={e => setEmail(e.target.value)}
/>
);
}
This is valid derived state because the component intentionally mirrors props.
Class Component Example (getDerivedStateFromProps)
class Example extends React.Component {
state = { value: null };
static getDerivedStateFromProps(props, state) {
if (props.value !== state.value) {
return { value: props.value };
}
return null;
}
render() {
return <p>{this.state.value}</p>;
}
}
⚠️ This method is rarely recommended in modern React.
Common Mistakes with Derived State
- Copying props into state unnecessarily
- Storing computed values instead of computing them
- Using derived state instead of lifting state up
These mistakes often appear in poorly structured applications and are discussed frequently in frontend architecture guides.
Best Practices & Special Notes
- Prefer computation over storage
- Keep a single source of truth
- Use
useMemofor expensive calculations - Avoid syncing props to state unless necessary
Testing your understanding with scenario-based questions on interactive quizzes can help avoid derived-state anti-patterns in real projects.
Final Takeaway
Derived state is powerful but dangerous when misused. If a value can be calculated, calculate it. Store state only when the data truly needs to be controlled independently. Mastering derived state is a key step toward writing clean, predictable, and professional React applications.