Build a TrafficLight component that cycles green → yellow → red → green…
Timings come from a config prop. The default is green 3000 ms, yellow 1000 ms, red 4000 ms, and each state names the next one:
const DEFAULT_CONFIG = {
green: { duration: 3000, next: 'yellow' },
yellow: { duration: 1000, next: 'red' },
red: { duration: 4000, next: 'green' },
};
The component must work with any config of this shape (for example, a pedestrian light with only red and green).
It starts in green, or in the initial prop if one is given.
Render three vertical lamps. Only the active lamp is lit.
A Pause / Resume button stops and restarts the cycle. Moving content needs a way to pause it.
Accessibility: the light is a single image with an accessible name that reflects its state, for example role="img" with aria-label="Traffic light: green". Don't use a live region that announces every change.
No leaked timers when the component unmounts or the config changes.
setInterval or chained setTimeout? Why?Spotted a mistake or a rights concern? Report an issue