Dev HelpersOpen library
← Back

Quantity stepper

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

Open this example in the interactive library

Live preview

Source code

// components/QuantityStepper.tsx

import { useState } from "react";

export default function QuantityStepper({
  value = 24,
  unit = "kg",
}: { value?: number; unit?: string } = {}) {
  const [n, set] = useState(Math.max(0, value));
  return (
    <div>
      <button
        aria-label="Decrease quantity"
        disabled={n <= 0}
        onClick={() => set(n - 1)}
      >
        −
      </button>
      <output>
        {n} {unit}
      </output>
      <button aria-label="Increase quantity" onClick={() => set(n + 1)}>
        +
      </button>
    </div>
  );
}