Sub topic 5
Designing what goes in and what comes out
This chapter assumes you know useState, the Rules of Hooks, and how to extract logic into a hook such as useToggle. A hook is a function, so its parameters and return value are its public API. Callers depend on that API, so it is worth designing on purpose.
Designing the inputs
For one or two required values, use positional parameters. useDebouncedValue(value, delay) reads clearly at the call site.
When a hook has several optional settings, take a single options object. Callers then pass only what they need, in any order, and you can add options later without breaking existing calls.
import { useState, useCallback } from 'react';
export function useCounter({ initial = 0, step = 1, min = -Infinity, max = Infinity } = {}) {
const [count, setCount] = useState(initial);
const increment = useCallback(() => setCount((c) => Math.min(c + step, max)), [step, max]);
const decrement = useCallback(() => setCount((c) => Math.max(c - step, min)), [step, min]);
const reset = useCallback(() => setCount(initial), [initial]);
return { count, increment, decrement, reset };
}Calling it with useCounter({ step: 5, max: 20 }) is easier to read than useCounter(0, 5, -Infinity, 20). The = {} default lets callers write useCounter() with no arguments.
Two input rules apply to every hook:
- Do not mutate the arguments you receive. They belong to the caller.
- Treat arguments as reactive. If a value can change between renders, your hook must handle the change, usually through dependency arrays. The chapter on dependency arrays covers this.
Designing the outputs
You have two main shapes to choose from.
| Shape | Best for | Caller experience |
|---|---|---|
| Tuple (array) | One or two values, used often | The caller names the values: `const [open, toggleOpen] = useToggle()` |
| Object | Three or more values, or optional ones | The caller picks by name: `const { count, reset } = useCounter()` |
A tuple lets callers rename freely, which matters when they use the same hook twice. This is why useState returns one. An object is safer once there are several values, because callers cannot mix up the order and can ignore values they do not need.
Keeping the contract small
A good hook API is easy to use and hard to misuse. These habits help:
- Return the same shape on every render. Do not return
nullat first and an object later. Return an object whose fields arenullor empty until the data exists. - Return answers, not raw material. If every caller computes
count >= max, returnisAtMaxfrom the hook. - Expose actions, not setters, when rules matter.
useCounterreturnsincrementinstead ofsetCount, so theminandmaxlimits cannot be bypassed. - Keep names consistent. Use
isXfor booleans and verbs for functions.
import { useCounter } from './useCounter';
function Quantity() {
const { count, increment, decrement } = useCounter({ initial: 1, min: 1, max: 10 });
return (
<>
<button onClick={decrement}>-</button>
<span>{count}</span>
<button onClick={increment}>+</button>
</>
);
}Exercise
A hook currently returns [value, setValue, isDirty, reset]. Callers often need only value and reset, and they sometimes mix up the order. Which return shape would you switch to, and why?
Show answer
Switch to an object: { value, setValue, isDirty, reset }. With four values, a tuple forces callers to remember positions and to skip unused ones with commas, such as [value, , , reset]. An object lets them pick value and reset by name, and you can add fields later without breaking existing callers.