Custom hooks

A custom hook is a function whose name starts with use and that calls other hooks to reuse stateful logic between components. Each call creates its own state. It follows the rules of hooks: called at the top level, never inside a condition or a loop.

Example

function useToggle(init = false) {
  const [on, setOn] = useState(init);
  return [on, () => setOn(v => !v)];
}

Related concepts

The whole glossary