Sub topic 14
Prop getters
This chapter assumes you can write custom hooks with useState or useReducer, spread props in JSX, and pass event handlers. You should also know why onClick on a custom component needs care.
The problem prop getters solve
A custom hook that shares toggle logic usually returns on and toggle. Every consumer must then wire them to an element by hand. That means setting onClick and the right ARIA attributes each time, and it is easy to forget one.
If the consumer also needs its own onClick, a plain object of props causes a collision. Spreading {...togglerProps} after the consumer's handler replaces it. Spreading it before replaces the hook's handler instead.
A prop getter is a function returned by the hook. It takes the consumer's props and returns a complete set of props with the hook's behavior merged in.
A hook with a prop getter
import { useState } from 'react';
export function useToggle({ initialOn = false } = {}) {
const [on, setOn] = useState(initialOn);
const toggle = () => setOn((prev) => !prev);
function getTogglerProps({ onClick, ...rest } = {}) {
return {
'aria-pressed': on,
onClick: (event) => {
onClick?.(event);
toggle();
},
...rest,
};
}
return { on, toggle, getTogglerProps };
}getTogglerProps pulls onClick out of the arguments. The returned onClick calls the consumer's handler first, then the hook's toggle. Both run, in a known order.
Using it
import { useToggle } from './useToggle';
export function SaveButton() {
const { on, getTogglerProps } = useToggle();
return (
<button
{...getTogglerProps({
onClick: () => console.log('clicked'),
className: 'btn',
})}
>
{on ? 'Saved' : 'Save'}
</button>
);
}The consumer gets aria-pressed, the merged click handler, and its own className from a single spread. The same getter works on a div or any other element, because the hook never renders markup.
Common mistakes
- Spreading after the getter. Writing
<button {...getTogglerProps()} onClick={myHandler}>overrides the merged handler and breaks the toggle. PassmyHandlerinto the getter instead. - Spreading
restlast by accident. In the hook above,...restcomes last, so a consumer can overridearia-pressed. That can be intended. If it is not, put...restfirst. - Forgetting the event. Call the consumer's handler with the same
eventargument it would normally receive. - Returning a stable handler. The getter creates a new function each render. That is fine for DOM elements. It matters only if you pass the result to a memoized child.
Exercise
Add a second getter, getContentProps, to useToggle. It should return hidden: !on and merge any props the consumer passes. Why is it better to return these props from a getter than to ask the consumer to write hidden={!on}?
Show answer
Add function getContentProps(props = {}) { return { hidden: !on, ...props }; } and return it from the hook. A getter keeps the hook's rules in one place, so a consumer cannot forget or misspell the prop. It also lets the hook change which props it needs later without changing consumer code.