A rectangle's diagonal can be calculated from its two side lengths. Keeping this as a derived value avoids synchronizing a third piece of state.
Worked example
// components/RectangleDiagonal.tsx
export default function RectangleDiagonal({
width = 12,
height = 8,
}: { width?: number; height?: number } = {}) {
return <p>Diagonal: {Math.hypot(width, height).toFixed(2)} units</p>;
}Implementation notes
For validation, compare the derived value with a known right triangle. The component should format the result only after performing the calculation.
Try it yourself
Change just the width and check that the diagonal updates.
Open the React example →Editorial date May 2, 2026 · Published December 6, 2026