Compare headless and render-prop patterns
Separate behavior from appearance with a headless Hook
A headless primitive exposes state and actions without imposing visual markup. A custom Hook can be a small headless primitive. A render prop is a function passed to a component so that the component can supply data while the caller selects markup. Higher-order components (HOCs) wrap component types to supply behavior or props; they remain relevant when maintaining older code. These patterns solve different ownership and customization problems. Start with a simple Hook and ordinary composition before introducing wrappers. A state reducer is an optional pure policy that accepts or revises a proposed transition.
export function nextOpen(open, action, reduce = (_previous, proposed) => proposed) { if (typeof open !== 'boolean') throw new TypeError('Expected boolean state'); let proposed; switch (action) { case 'toggle': proposed = !open; break; case 'close': proposed = false; break; case 'open': proposed = true; break; default: throw new RangeError('Unknown action'); } const next = reduce(open, proposed, action); if (typeof next !== 'boolean') throw new TypeError('Reducer must return boolean'); return next;}nextOpen computes a proposed boolean from an action and lets a policy return the final state. It has no React dependency or side effects. For example nextOpen(false, "toggle") returns true, while nextOpen(true, "close", old => old) returns true because the policy refuses the change. Reject invalid actions and policies. A veto policy is only a UI behavior rule; it does not authorize or deny real access. Avoid trapping a user in a dismissible dialog by vetoing all close actions.
Expose a clear controlled or uncontrolled contract
import React, {useId, useState} from 'react';import {nextOpen} from './disclosure-model.mjs';type Options = {open: boolean; onOpenChange: (value: boolean) => void; defaultOpen?: never} | {open?: never; defaultOpen?: boolean; onOpenChange?: (value: boolean) => void};export function useDisclosure(options: Options = {}) { const [local, setLocal] = useState(options.defaultOpen ?? false); const open = options.open ?? local; const panelId = useId(); function toggle() { const next = nextOpen(open, 'toggle'); if (options.open === undefined) setLocal(next); options.onOpenChange?.(next); } return {open, panelId, toggle};}export function HeadlessDemo() { const {open, panelId, toggle} = useDisclosure(); return <section><button type="button" aria-expanded={open} aria-controls={panelId} onClick={toggle}> Tool availability</button><div id={panelId} hidden={!open}>Three clamps available.</div></section>;}When open is supplied, the parent owns state and must update it via onOpenChange. Otherwise defaultOpen is read on initial mount and local state owns the value. Do not switch ownership mode during a mounted instance. This small API computes a proposal from the current render snapshot and expects one action per ordinary user activation; it is not a general queued-action API for several toggles in one event. Use a reducer or functional updates for that requirement. The consumer must still provide the native button, matching IDs and hidden panel. A headless library does not automatically make every consumer accessible.
Choose render props or wrappers only for a clear benefit
import React from 'react';import type {ComponentType, ReactNode} from 'react';import {useDisclosure} from './Headless';type View = ReturnType<typeof useDisclosure>;function DisclosureView({render}: {render: (view: View) => ReactNode}) { const view = useDisclosure(); return <>{render(view)}</>;}function withWorkshopNotice<P extends object>(Component: ComponentType<P>) { return function WithWorkshopNotice(props: P) { return <section><p>Workshop information</p><Component {...props} /></section>; };}function Supplies() { return <DisclosureView render={({open, panelId, toggle}) => <> <button type="button" onClick={toggle} aria-expanded={open} aria-controls={panelId}>Supplies</button> <div id={panelId} hidden={!open}>Glue and brushes</div></>} />;}export const WorkshopSupplies = withWorkshopNotice(Supplies);The render prop receives the same state and actions as the Hook. It decides what to display and must not call Hooks itself as if it were a component. The HOC adds surrounding markup and is created outside rendering to preserve component type identity. A generic wrapper does not automatically preserve statics or ref contracts; verify the version-specific ref behavior before exposing it as a library API. Polymorphic components likewise need a constrained element and prop contract: a link navigates, a button performs an action. Do not substitute a generic div for either. Choose compound context for cooperating parts, named slots for fixed layout, Hooks for reusable behavior, render props for caller-driven rendering, and HOCs for compatible legacy integrations. Test the public behavior, not the chosen implementation pattern. Practice: implement a parent-controlled HeadlessDemo variant and explain how a refused parent update affects the displayed panel.