Sub topic 3
Derive, don't duplicate: the temperature converter
Assume you know function components, JSX, props, useState, and event handlers. This chapter focuses on deciding where state belongs and deriving values from it.
Derive, don't duplicate: the temperature converter
A temperature converter should store one source value and its scale. It should derive the other temperature during rendering. Storing both values creates two copies that can disagree.
import { useState } from 'react';
function convert(value, formula) {
const number = parseFloat(value);
return Number.isNaN(number)
? ''
: String(Math.round(formula(number) * 100) / 100);
}
export default function Calculator() {
const [state, setState] = useState({ scale: 'c', value: '' });
const celsius = state.scale === 'c'
? state.value
: convert(state.value, fahrenheit => ((fahrenheit - 32) * 5) / 9);
const fahrenheit = state.scale === 'f'
? state.value
: convert(state.value, celsius => (celsius * 9) / 5 + 32);
return (
<>
<label>
Celsius:
<input
value={celsius}
onChange={event => setState({ scale: 'c', value: event.target.value })}
/>
</label>
<label>
Fahrenheit:
<input
value={fahrenheit}
onChange={event => setState({ scale: 'f', value: event.target.value })}
/>
</label>
</>
);
}The input that was edited remains the source value. The other input is calculated from it.
Deciding which component should own state
State belongs in the closest component that needs to coordinate all its readers and writers. If only one component uses a value, keep it local. If siblings need the same value, move it to their closest common parent.
Pushing state back down when it no longer needs sharing
Lifting state is not permanent. If a parent no longer coordinates a value, move that state back into the component that uses it. This reduces props, rerenders, and unrelated responsibilities.
When lifting gets heavy: prop drilling
Prop drilling means passing a value through components that do not use it, only to reach a deeper child. A few levels are clear and useful. Many levels suggest composition or context.
> callout tip > Keep the data flow explicit first. Introduce a broader state mechanism only when the sharing problem is real.
Alternatives: composition, context, and state libraries
Composition passes a ready-made child or render function, avoiding intermediate props. Context provides a value to many descendants without passing it at every level. A state library can organize complex shared state, server synchronization, or cross-page workflows. Each adds indirection, so choose the smallest suitable tool.
Common pitfalls and how to debug them
- Do not store values that can be calculated from existing state.
- Keep input state as text when users are typing; convert it only for calculations.
- Pass the parent's value and update callback together.
- Check which component owns each value when the screen is stale.
- Use React DevTools to inspect props and state, then log the event value and derived result.
Exercise
Why does the converter store the last edited scale and value instead of both temperatures?
Show answer
Those two fields are the source of truth. The other temperature can be derived from them, so storing it separately could create inconsistent copies.