Dev Helpers
← Back

Bounded rating control

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

Open this example in the interactive library

Live preview

Source code

// components/BoundedRatingControl.tsx

import { useState } from "react";

export default function BoundedRatingControl({
  initial = 0,
}: { initial?: number } = {}) {
  const [rating, set] = useState(initial);
  return (
    <fieldset>
      <legend>Rating</legend>
      {[1, 2, 3, 4, 5].map((n) => (
        <button
          type="button"
          key={n}
          aria-pressed={rating === n}
          onClick={() => set(n)}
        >
          {n}
        </button>
      ))}
    </fieldset>
  );
}