Sub topic 2
What a custom hook is
A custom hook is a JavaScript function whose name starts with use and that calls other hooks inside its body. This chapter covers what that definition means, why the naming convention matters, and how a hook differs from a component and from a plain function.
This chapter assumes you know function components and JSX, props, useState and useEffect basics, and modern JavaScript (arrow functions, destructuring, closures). It also assumes you know the Rules of Hooks: call hooks only at the top level of a component or hook, and only from React functions.
A function that calls hooks
Built-in hooks such as useState can only run while React is rendering a component. A custom hook is a function that lets you reuse a group of built-in hook calls under one name. Here is a minimal one:
import { useState, useCallback } from 'react';
export function useToggle(initial = false) {
const [on, setOn] = useState(initial);
const toggle = useCallback(() => setOn((value) => !value), []);
return [on, toggle];
}A component calls it like any built-in hook:
import { useToggle } from './useToggle';
export function Menu() {
const [open, toggleOpen] = useToggle();
return (
<button onClick={toggleOpen}>{open ? 'Close menu' : 'Open menu'}</button>
);
}When Menu renders, React runs useToggle as part of that render. The useState and useCallback calls inside belong to the Menu component, in the same call order as if you had written them directly in Menu.
Why the `use` prefix matters
React does not detect custom hooks by inspecting your code at runtime. The prefix is a convention that tools rely on. The eslint-plugin-react-hooks linter treats any function named use followed by a capital letter as a hook. It then enforces the Rules of Hooks inside it and checks the dependency arrays of effects.
If you name the same function toggleState, the linter stops checking it, and a misplaced hook call inside it could go unnoticed until it breaks at runtime. Name it useToggle and the checks apply.
The reverse also holds. A function that calls no hooks should not start with use. Naming it that way suggests it has hook restrictions when it does not.
How hooks differ from components and plain functions
| Row | Component | Custom hook | Plain function |
|---|---|---|---|
| Name | Starts with a capital letter | Starts with `use` | Any name |
| Returns | JSX (or `null`) | Any value you choose | Any value |
| Can call hooks | Yes | Yes | No |
| Called by | React, through JSX | Components or other hooks | Anything |
A hook returns whatever suits the caller: a value, a tuple, an object, or nothing at all. A component must return something React can render.
What a custom hook does not do
A hook adds no new React feature. It cannot do anything you could not do by writing the same hook calls directly in the component. It also does not change the component tree, so you do not need wrapper components or render props to reuse logic.
Calling a hook does not share state between components either. Each call creates its own state, which the next chapters cover.
Exercise
A teammate writes function formatPrice(value) { const [locale] = useState('en-US'); return value.toLocaleString(locale); } and calls it from a component. Which two problems does the name cause or hide, and how do you fix it?
Show answer
The function calls a hook, so its name should start with use (for example useFormatPrice). Without the prefix, the linter will not check it against the Rules of Hooks. The hook call is also unnecessary, because the locale never changes. A plain function that takes locale as a parameter would be simpler and could be called anywhere.