Every codebase has that one hooks file:
function useEverything(query: string) {
// fetches + debounces + caches + logs analytics
// + syncs to the store + manages loading + errors
return { a, b, c, d, e, f, g, h, i, j };
}Six hundred lines, one hook, eleven responsibilities, a useEffect nobody has read since 2023. It works, mostly, and everyone routes around it. Custom hooks are the best abstraction React offers and the easiest one to ruin, because nothing forces them to stay honest. These five rules do.
Step 1: One job per hook
If you cannot describe what the hook does without the word "and", it is multiple hooks:
// wrong: four hooks in a trench coat
const everything = useEverything(query);
// right: composed at the call site
const debounced = useDebounced(query, 300);
const user = useUser(id);Composition happens where the component combines useDebounced with useFetch and keeps both legible. The test: when a bug report arrives, can you name the file from the symptom? With single-purpose hooks, you can. With soup, you open the file and start reading.
Step 2: Return a shape, not a pile
Arrays only work when the order is obvious and stable, which describes useState and almost nothing else:
// wrong: order breaks every call site on refactor
return [value, setValue, loading, error, data, refetch];
// right: named fields survive refactors
return { value, status, data, refetch };Ten loose values from one hook means every call site is a destructure waiting to break, and the return type needs a paragraph comment. That comment is the hook telling you it does too much.
Step 3: Make dependencies honest
Every value the hook reads comes in through arguments:
// wrong: hidden module-level read,
// behaves differently per call site
function useFiltered() {
const q = store.query;
return items.filter(matches(q));
}
// right: every input is an argument
function useFiltered(items: Item[], q: string) {
return items.filter((i) => matches(i, q));
}Hidden reads of singletons, magic defaults that differ per call site, "it knows the current route internally": all of it makes the hook behave differently depending on where it runs. The whole value of a hook is that its behavior is a function of its inputs. Keep it that way and hook bugs become reproducible instead of mystical.
Step 4: Handle the race you know is there
If the hook does async work, it needs a cancel path:
// wrong: stale response lands last and wins
useEffect(() => {
fetch(`/api/search?q=${query}`)
.then((r) => r.json())
.then(setResults);
}, [query]);
// right: abort the old request on cleanup
useEffect(() => {
const ctrl = new AbortController();
fetch(`/api/search?q=${query}`, { signal: ctrl.signal })
.then((r) => r.json())
.then(setResults)
.catch((e) => {
if (e.name !== "AbortError") setError(e);
});
return () => ctrl.abort();
}, [query]);Without it: user types, dependencies change, the old slow response lands last and overwrites the fresh one. Every unfinished fetching hook ships this bug; the only question is whether a user finds it on a slow network day or you find it in review.
Step 5: Test it like a component
renderHook from React Testing Library, mocked inputs, assertions across rerenders:
test("ignores stale responses", async () => {
const { result, rerender } = renderHook(
({ q }) => useSearch(q),
{ initialProps: { q: "re" } },
);
rerender({ q: "react" });
await waitFor(() =>
expect(result.current.data).toEqual(reactResults),
); // the "re" response never overwrites "react"
});The test is what catches rule four's race on purpose instead of in production. And a hook that resists being tested in isolation is announcing that it has too many responsibilities, which makes this rule a design review as much as a test.
Five rules, none of them clever. Small hooks with honest inputs, a clean return shape, a cancel path, and a test. If your codebase has a hooks file everyone fears, these are the five checks that take it apart.
Maintaining a React codebase that grew faster than its hooks? I clean these up for a living. Tell me about the app and I will tell you which hook to refactor first.