Derived state is state that is computed from props or user input instead of being stored independently.
It helps keep the UI consistent with incoming data, but if used incorrectly it can cause unnecessary re-renders, duplicated state, or stale values.
Use derived state when a component needs to respond to changes in props or related user inputs, while still keeping the logic predictable and easy to understand.
useEffect to sync derived state only when necessary (e.g., when props change).useMemo() to cache derived values.Imagine a parent passes a price prop, and the child needs to display the price including tax (for example, 18% GST).
// React component that derives final price (with tax) from a price prop
function PriceDisplay({ price }) {
const [finalPrice, setFinalPrice] = React.useState(price * 1.18);
React.useEffect(() => {
setFinalPrice(price * 1.18); // Update derived state whenever the price prop changes
}, [price]);
return <h3>Final Price: ₹{finalPrice.toFixed(2)}</h3>;
}
If the parent passes price = 1000, the component will compute:
finalPrice = 1000 * 1.18 = 1180If later the parent updates price to 1500, the useEffect runs again and recomputes finalPrice using the latest prop.
In many cases, you don’t need separate state for derived values. You can calculate them directly inside the render function.
// Simpler version: derive finalPrice directly during render
function PriceDisplay({ price }) {
const finalPrice = price * 1.18;
return <h3>Final Price: ₹{finalPrice.toFixed(2)}</h3>;
}
This approach is enough when:
Only store derived values in state if you plan to let the user modify them or you must reuse them across multiple renders in a way that can’t be recomputed easily.
You can also derive state based on user actions, like keeping two related inputs (Celsius and Fahrenheit) in sync.
// Temperature converter that derives Fahrenheit from the Celsius input
function TemperatureConverter() {
const [celsius, setCelsius] = React.useState(0);
const [fahrenheit, setFahrenheit] = React.useState(32);
const handleCelsiusChange = (e) => {
const c = e.target.value;
setCelsius(c);
setFahrenheit((c * 9) / 5 + 32); // Derive Fahrenheit from the updated Celsius value
};
return (
<div>
<input type="number" value={celsius} onChange={handleCelsiusChange} />
<p>Fahrenheit: {fahrenheit}</p>
</div>
);
}
When the user types 0 in the Celsius input, Fahrenheit shows 32. If the user types 100, the displayed Fahrenheit becomes 212.
The key idea: fahrenheit is always derived from the current celsius value.
Sometimes you want to:
// Editable title that stays in sync with the parent prop
function EditableTitle({ title }) {
const [text, setText] = React.useState(title);
React.useEffect(() => {
setText(title); // Refresh local state whenever the title prop changes
}, [title]);
return (
<div>
<input value={text} onChange={(e) => setText(e.target.value)} />
<p>Preview: {text}</p>
</div>
);
}
The user can freely edit the input, and the preview updates immediately.
If the parent sends a new title prop (for example, after loading data), the useEffect hook will sync the local text state with the new title.
useEffect with a clear dependency array.useMemo() to avoid recalculating on every render.basePrice and displays the final price after a 10% discount.basePrice changes from the parent.defaultText using useEffect.Goal: Get comfortable creating and managing derived state from props or user input while keeping React components predictable and efficient.