All topics
Patternsadvanced

Prop Getters Pattern

Learn the prop getters pattern used by headless UI libraries to bundle correct accessibility and event-handling props onto elements.

A prop getter is a function returned by a hook (commonly used in 'headless UI' libraries) that computes and returns the full set of props — event handlers, ARIA attributes, refs — that need to be spread onto a specific DOM element for a component to work correctly. Instead of the consumer manually wiring up onClick, onKeyDown, aria-expanded, and other details themselves, they simply call getToggleButtonProps() and spread the result onto their button.

A prop getter is like a stagehand who hands an actor (the DOM element) a fully prepared prop bundle — including required safety gear (accessibility attributes) — but will happily tuck in any personal item you also want the actor carrying (your own onClick), rather than making you choose between the stagehand's setup and your own.

Key Concepts

1
This pattern solves a subtle problem: if a hook just returned raw values and handlers separately, and the consumer needed to add their *own* onClick behavior too (like logging analytics) alongside the hook's required behavior, they'd have to remember to call both functions themselves and merge other props correctly. Prop getters accept the consumer's own props as an argument and internally compose them with the required internal behavior, correctly merging event handlers (calling both the internal one and the user's one) rather than one silently overwriting the other.
onClick
2
Libraries like Downshift (autocomplete/combobox) and many other 'headless' component libraries (which provide only behavior and accessibility wiring, no visual styling) rely heavily on this pattern, since it lets them guarantee correct ARIA attributes and keyboard interactions are always present regardless of how a consumer chooses to style the actual markup.
3
Interviewers exploring advanced component API design sometimes ask candidates to explain why a prop getter function is preferable to just returning a plain object of props directly, expecting an answer centered on safely merging consumer-supplied handlers with the hook's own required handlers rather than one clobbering the other.