Dev Helpers

React / Geometry

Rectangle diagonal

Dev Helpers · May 2, 2026

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

Related articles

Percentage meter

A percentage meter needs both a bounded visual fill and a meaningful textual value. Handle a zero denominator before dividing.

Vertical typesetting sample

Vertical typesetting changes the reading direction and available inline space. Treat the sample text as content so it remains selectable and accessible.

Square cover crop

A square image crop gives a card grid consistent geometry. The crop anchor determines whether the most important part of a photo survives.