Home / Learn React / What Are Hooks? Rules of Hooks

What Are Hooks? Rules of Hooks

Hooks are special functions that start with use. They let a function component keep state, run side effects, read context and more.

In shortCall hooks only at the top level of a component or custom hook, never inside if, loops or nested functions.

Theory in plain words

Hooks you will learnState: useState, useReducer, useOptimistic, useActionStateEffects: useEffect, useLayoutEffect, useEffectEventContext and refs: useContext, useRef, useImperativeHandlePerformance: useMemo, useCallback, useTransition, useDeferredValueOthers: useId, useSyncExternalStore, use, useFormStatus, useDebugValue
Hooks you will learn

What it is

A hook is a function that lets your component "hook into" React features. useState remembers values, useEffect runs code after render, useContext reads shared data.

Why we use it

Before hooks only class components could have state and lifecycle methods. Hooks give function components the same power with less code, and let you share logic between components with custom hooks.

How it works

React remembers hook state by the order in which hooks are called during a render. That is why the order must be the same every time: no hooks inside conditions or loops.

Where it is used

  • Every modern React app
  • Sharing logic: useFetch, useLocalStorage, useDebounce
  • Libraries: React Router, TanStack Query and Redux all offer hooks

The two rules

  1. Only call hooks at the top level. Not inside if, loops, nested functions or after an early return.
  2. Only call hooks from React functions: function components or your own custom hooks (names starting with use).
Break the rule on purposeTry it Yourself

React notices that the number of hooks changed between renders and throws an error.

Why the order matters

React does not know hook names. It stores your hooks in a list and matches each call to a slot by its position. If a condition skips a call, every following hook gets the wrong slot.

Order is the identity1st call = slot 0(name state)2nd call = slot 1(age state)3rd call = slot 2(effect)Same orderevery render
Order is the identity
Tip:The ESLint plugin eslint-plugin-react-hooks checks both rules for you. Vite templates include it.
Careful:To use state conditionally, put the condition inside the hook or in a child component, not around the hook call.

Quick quiz

1. Where may you call a hook?

2. Why does the order of hooks matter?

3. A custom hook name must start with...

Common questions

Where may you call a hook?

At the top level of a component or custom hook. Hooks must be called in the same order every render.

Why does the order of hooks matter?

React matches hooks to stored state by call order. Hook state is kept in a list by position.