The Prop Collections & Getters pattern helps hooks expose flexible, accessible, and easy-to-use APIs for UI interactions.
When building reusable hooks, users must remember to wire things correctly:
<button aria-pressed={on} onClick={toggle}>On</button>
This becomes error-prone with:
aria-*)A prop collection is a ready-made props object returned by a hook.
function useCounter() {const [count, setCount] = useState(0)return {count,counterProps: {children: count,onClick: () => setCount(c => c + 1),},}}
Usage:
<button {...counterProps} />
✅ Easy for common cases.
❌ Hard to customize safely
A prop getter is a function that merges user props with internal logic.
function useToggle() {const [on, setOn] = useState(false)const toggle = () => setOn(o => !o)const getTogglerProps = ({ onClick, ...props } = {}) => ({'aria-pressed': on,onClick: e => {onClick?.(e)toggle()},...props,})return { on, getTogglerProps }}
Usage:
<button {...getTogglerProps({ onClick: () => console.log('custom') })} />
Now:
Use Prop Collections/Getters when: