A custom hook is a function whose name starts with use and that calls other hooks. It's how React shares logic between components: an online-status subscription, a form field's state and handler, a data-fetching effect.
It doesn't share state. The hook's body runs inside the render of whichever component calls it, so its useState and useEffect calls become that component's own hooks. Two components calling useCounter() get two separate counts; one component calling it twice gets two as well. Copying the hook's body into the component would behave identically. React's docs: "Each call to a Hook is completely independent from every other call to the same Hook."
Callers see the same data only when the hook reads something that lives outside them: a context (then it's the provider's state), an external store, the browser. Two components using useOnlineStatus() agree because both subscribe to the same browser events, but each holds its own state and its own subscription. To share state itself, lift it up or put it in context.
Because the body runs on every render, a custom hook is part of the component: it must be pure during render, it always receives the latest props and state, and the functions it returns are new each render unless memoized.
The name matters. The docs: "Hook names must start with use followed by a capital letter." Tools rely on it:
- The
rules-of-hookslint rule allows hook calls only in components andusefunctions, and checks thatusefunctions are called unconditionally. - The React Compiler, by default, compiles functions named like components or hooks.
A function that calls no hooks should not be named use…: it could then be called in a condition, and the name would mislead.