mediumMachine Coding

Build a Star Rating component

639 views
01

Understand the problem

Build a reusable 5-star rating with hover, click and half-star support.

ReactEventsHalf Star
02

Attempt it yourself

Sketch your approach before reading the solution — that's what interviews test.

Stuck? AI Nudge Available

Get a conceptual hint to guide your logic without spoiling the final implementation.

03

Study the solution

Solve in

The solution is waiting

Give it an honest attempt first — then compare your thinking with the full walkthrough.

04

React solution

Complete solutionRun Playground
import { useState } from 'react';
import StarRating from './src/StarRating';

export default function App() {
  const [rating, setRating] = useState(2.5);
  return (
    <div style={{ fontFamily: 'system-ui, sans-serif', padding: 32, textAlign: 'center' }}>
      <h2 style={{ marginBottom: 4 }}>Rate your experience</h2>
      <p style={{ color: '#666', marginTop: 0, marginBottom: 16 }}>Hover the left vs right half of a star. Arrow keys work too.</p>
      <StarRating value={rating} precision={0.5} onChange={setRating} />
      <p style={{ marginTop: 18, fontSize: 18 }}>You rated: <strong>{rating}</strong> / 5</p>
      <hr style={{ margin: '24px auto', maxWidth: 240, border: 'none', borderTop: '1px solid #e5e5ea' }} />
      <p style={{ color: '#666', marginBottom: 6 }}>Read-only display (3.5):</p>
      <StarRating value={3.5} readOnly size={24} />
    </div>
  );
}
05

Join the discussion

Discussion (0)

Sign in to join the discussion.

No responses yet. Be the first to share what you think.