What is Derived State?
You'll learn the difference between state you store and values you calculate from that state.
The Core Idea
Derived state is any value that can be computed directly from existing props or state, rather than stored in its own separate state variable.
Think of it like a spreadsheet: if cell C1 contains =A1 + B1, you don't also type the sum into C1. The sum is derived from A1 and B1 automatically. In React, the same principle applies—if you can calculate a value from data you already have, you shouldn't store it separately.
What Counts as Derived?
Here are common examples:
- Filtered lists: You have an array in state and a search term—the filtered results are derived
- Computed totals: If you have prices in state, the sum is derived
- Boolean flags: Like
isEmpty = items.length === 0 - Formatted strings: Like
fullName = firstName + " " + lastName
Why This Matters
When you store what should be derived state, you create two sources of truth. Now you have to keep them in sync manually, often using useEffect, which causes extra renders and introduces bugs.
The correct approach: calculate derived values directly during render. Let React handle the rest.
Key Takeaway: Derived state is computed from existing data during render, not stored separately. If you can calculate it, don't store it.