Dev Helpers
← Back

Hydration calculator

Import and render <HydrationCalculator /> in a React project.

Open this example in the interactive library

Live preview

Source code

// components/HydrationCalculator.tsx

import { useState } from "react";

export default function HydrationCalculator({
  grams = [120, 80, 50],
}: { grams?: number[] } = {}) {
  const [water, set] = useState(75);
  return (
    <label>
      Water (g)
      <input
        type="number"
        min={0}
        value={water}
        onChange={(e) => set(+e.target.value)}
      />
      <output>{grams[0] ? ((water / grams[0]) * 100).toFixed(1) : 0}% hydration</output>
    </label>
  );
}