Star rating
Your code isn't saved yet: refreshing or leaving the page resets it. Saved progress comes with user accounts.
Build a reusable StarRating component. App.js already uses it twice, a 5-star rating and a 10-star score, and the Star icon is ready. Right now StarRating just shows two fixed stars.
<StarRating value={rating} onChange={setRating} />
<StarRating max={10} value={score} onChange={setScore} />Requirements
- Render
maxstars (default5). Stars up tovalueare filled, the rest are empty. - The component is controlled: the parent owns
value, and the component callsonChange(newValue)when the user picks a rating. - Hovering a star previews that rating: it and every star before it fill. Moving the mouse away shows
valueagain. - Clicking a star sets the rating to that star. Clicking the star that is already the rating clears it to
0. - The two ratings on the page are independent.
Accessibility
- Each star is a
<button>with a label such as3 stars, so it is reachable withTaband announced clearly. - Keyboard focus previews a rating the same way hover does.
Note
The preview updates as you type. There are no automated tests for this challenge yet: hover, click and tab through both ratings, then compare with the reference solution.
Follow-ups interviewers ask
- Support half stars.
- Add a
readOnlymode for displaying an average rating. - Implement it as a radio group (
role="radiogroup") with arrow-key navigation, like native radio buttons.
The approach
There are two pieces of state, and they belong in different places:
- The rating belongs to the parent. The component receives it as
valueand reports changes throughonChange. That makes it controlled, so the same component works in a form, with a reset button, or with a value loaded from a server. - The hover preview is purely visual and nobody outside cares about it, so it stays local in
useState.
What to draw is then one line: const shown = hovered ?? value. A star is filled when its number is <= shown.
Details that matter
Array.from({ length: max }, ...)builds the stars from themaxprop, so the 10-star score needs no extra code.onMouseLeavesits on the container, not on each star. Moving between stars never flickers back tovalue, and leaving the whole component resets the preview.??and not||: a hovered star is never0, but using??states the intent that only "no hover" (null) falls back tovalue.- Clicking the current rating calls
onChange(0). Without this, a user can never get back to "not rated".
Accessibility
Each <button> gets aria-label="3 stars", because the SVG is decorative (aria-hidden). aria-pressed marks the star that matches the current rating. onFocus and onBlur mirror the hover behaviour for keyboard users.
What interviewers look for
- Controlled vs. uncontrolled, and why the rating is controlled here.
- Deriving what to display instead of storing it.
- A reusable API:
max,valueandonChange, with sensible defaults.
Reference solution
src/StarRating.js
import { useState } from 'react'
import Star from './Star'
export default function StarRating({ max = 5, value, onChange }) {
// Only the hover preview is local: the rating itself is owned by the parent.
const [hovered, setHovered] = useState(null)
const shown = hovered ?? value
return (
<div className="star-rating" onMouseLeave={() => setHovered(null)}>
{Array.from({ length: max }, (_, index) => {
const star = index + 1
return (
<button
key={star}
type="button"
className="star"
aria-label={`${star} ${star === 1 ? 'star' : 'stars'}`}
aria-pressed={star === value}
onMouseEnter={() => setHovered(star)}
onFocus={() => setHovered(star)}
onBlur={() => setHovered(null)}
// Clicking the current rating again clears it.
onClick={() => onChange(star === value ? 0 : star)}
>
<Star filled={star <= shown} />
</button>
)
})}
</div>
)
}