Dev Helpers
← Back

Discount comparison

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

Open this example in the interactive library

Live preview

Source code

// components/DiscountComparison.tsx

import { useState } from "react";

export default function DiscountComparison({ price = 24 }: { price?: number } = {}) {
  const [discount, set] = useState(10);
  return (
    <label>
      Discount %{" "}
      <input
        type="number"
        min={0}
        max={100}
        value={discount}
        onChange={(e) => set(Math.min(100, Math.max(0, Number(e.target.value))))}
      />
      <del>${price}</del> <strong>${(price * (1 - discount / 100)).toFixed(2)}</strong>
    </label>
  );
}