Sub topic 16
Headless components and hooks
This chapter assumes you know function components, useState, useReducer, and the custom hook and prop getter patterns from earlier chapters. It shows how those pieces combine into headless components and hooks.
What "headless" means
A headless component or hook supplies behavior and state but no markup and no styles. The consumer decides what to render. Accessibility attributes, keyboard handling, and state transitions live in the headless layer. Appearance lives in the consumer.
Compare this with a styled component library. A styled Switch ships a fixed look, and you override it with props or CSS. A headless useToggle ships only the logic, so you apply it to any element.
The pieces you already have
A headless hook is usually three earlier patterns together:
- A custom hook that owns the state.
- Prop getters that hand the consumer the props an element needs.
- A state reducer that lets the consumer change how transitions behave.
The useToggle hook from the previous chapters has all three. It returns on, toggle, and reset. It also returns getTogglerProps, which supplies aria-pressed and an onClick that merges the consumer's handler with the hook's own.
Using one hook with different markup
Because the hook renders nothing, the same logic can drive different elements. This example assumes the useToggle code from the prop getters chapter is saved in useToggle.js.
import { useToggle } from './useToggle';
export function Examples() {
const first = useToggle();
const second = useToggle({ initialOn: true });
return (
<div>
<button {...first.getTogglerProps()}>
{first.on ? 'Dark mode on' : 'Dark mode off'}
</button>
<span
role="button"
tabIndex={0}
{...second.getTogglerProps({
onClick: () => console.log('clicked'),
})}
>
{second.on ? '✔ Subscribed' : 'Subscribe'}
</span>
</div>
);
}Both elements get the same state logic. The second one passes its own onClick, and the prop getter calls it before toggling.
Note that a span with role="button" does not respond to the keyboard on its own. The consumer must add Enter and Space handling. A native button is usually the better choice. Headless libraries leave the element choice to you, so the accessibility of the final result is partly your job.
Headless components
A headless component is the component form of the same idea. It owns state and calls a render prop, or exposes compound subcomponents that render only the elements you give them. The Tabs compound component earlier in this tutorial is close to headless. It manages the selected value and ARIA roles, and the consumer supplies the layout and styling.
Libraries such as Radix UI, Headless UI, React Aria, and Downshift follow this approach. They implement the hard behavior once and leave the styling to you.
Trade-offs
| Benefit | Cost |
|---|---|
| Full control over markup and styling | The consumer writes more code than with a styled component |
| Behavior is reused across designs | The consumer can wire props to the wrong element and break accessibility |
| Easy to test the logic alone | The API surface (getters, reducer, control props) takes time to learn |
Exercise
Why does a headless hook return prop getters instead of ready-made JSX, and what must the consumer still check when using one on a non-button element?
Show answer
The hook renders no markup, so the consumer chooses the element and styling. The prop getter supplies the attributes and handlers that element needs, and merges the consumer's own handlers. On a non-button element such as a span, the consumer must still add keyboard support (Enter and Space) and a suitable role and tabIndex, because the hook cannot make the element accessible by itself.