React in Practice · 22 min · 170 XP
Accessible interaction
Build interactions from semantic controls, expose their state, and keep content relationships explicit.
Accessibility starts with the correct element. A button already supports focus, Enter, Space and disabled state. A clickable div supplies none of those guarantees, so rebuilding them by hand creates more code and usually misses something.
Semantic HTML sometimes needs a state relationship. A disclosure button uses aria-expanded to expose whether its panel is open and aria-controls to point at the panel. The panel can use aria-labelledby to point back to the button.
const id = useId();
<button
id={id + "-button"}
aria-expanded={open}
aria-controls={id + "-panel"}
>
Shipping details
</button>Use useId for relationships that need unique DOM IDs. Hard-coded IDs collide when a component appears twice; random IDs change between renders and can break server rendering.
Loading your workspace…