Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Next.js and React

ESLint says a React Hook is called conditionally, in useCallback or any other hook

ErrorReact Hook "useCallback" is called conditionally. React Hooks must be called in the exact same order in every component render.

Short answer

React matches hooks to state by call order, so every render must call the same hooks in the same order. The lint error names the hook that sometimes does not run. Find the early return, if, loop, try block or inner function above it, then move the hook above that line and put the condition inside the hook.

  • Applies to React 19, eslint-plugin-react-hooks, Node.js 22
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Run ESLint on the one file. The message carries a line number, and the last sentence of the message tells you which cause below you have.

    npx eslint src/ProductList.jsx
  2. 2Match what you see, then jump to its fix

    1. 1Did you accidentally call a React Hook after an early return?The hook sits below an early returnFix
    2. 2is called conditionally. React Hooks must be called in the exact same order in every component render.The hook is inside an if, a ternary or a short-circuit expressionFix
    3. 3is called in function "handleClick" that is neither a React function component nor a custom React Hook functionThe hook is called inside an event handler, a callback or another nested functionFix
    4. 4cannot be called in a try/catch block, or may be executed more than once. Possibly because it is called in a loop.The hook is inside a loop or a try/catch blockFix

What does the error look like?

  • React Hook "useCallback" is called conditionally. React Hooks must be called in the exact same order in every component render.
  • Did you accidentally call a React Hook after an early return?
  • React Hook "useCallback" is called in function "handleClick" that is neither a React function component nor a custom React Hook function.

Cause 1 of 4

The hook sits below an early return

1Check

Scroll up from the flagged line inside the same component. Any return, usually a loading or empty-state guard, above the hook makes that hook run only on some renders.

grep -n "return\|useCallback\|useMemo\|useEffect" src/ProductList.jsx

2Fix

Move every hook above the first return. Guards stay, but they come after all hook calls.

function ProductList({ items }) {
  const onPick = useCallback((id) => select(id), []); // hooks first
  if (!items) return null;                              // guard after
  return <List items={items} onPick={onPick} />;
}

3Verify

Lint the file again; the error for that line is gone.

npx eslint src/ProductList.jsx

Confirmed by react.dev

Did this fix it?

Cause 2 of 4

The hook is inside an if, a ternary or a short-circuit expression

1Check

Look at the line before the hook and the line before that. A hook written inside if (...) { }, after &&, or in one side of a ternary only runs when the condition is true.

grep -n "if (\|&& use\|? use" src/ProductList.jsx

2Fix

Call the hook every time, and move the condition into the function you give it, or into its body.

// before: if (enabled) { const save = useCallback(...) }
const save = useCallback(() => {
  if (!enabled) return;
  persist(draft);
}, [enabled, draft]);

3Verify

Toggle the condition in the browser and confirm the component renders without a hooks error in the console.

npx eslint src/ProductList.jsx

Confirmed by react.dev

Did this fix it?

Cause 3 of 4

The hook is called inside an event handler, a callback or another nested function

1Check

The message names the inner function. Hooks may only be called at the top level of a component or of a custom hook whose name starts with use.

grep -n "=> {\|function " src/ProductList.jsx

2Fix

Create the value with the hook at the top of the component, and use it inside the handler. If a helper function needs hooks, rename it to start with use and call it from the top level.

function ProductList() {
  const send = useCallback((id) => post(id), []);
  const handleClick = (id) => send(id);   // uses it, does not call a hook
  return <button onClick={() => handleClick(1)}>Buy</button>;
}

3Verify

Run the linter and click the control in the browser.

npx eslint src/ProductList.jsx

Confirmed by github.com

Did this fix it?

Cause 4 of 4

The hook is inside a loop or a try/catch block

1Check

A hook inside a for or while loop, or inside a try block, is not allowed; the rule reports both.

grep -n "for (\|while (\|try {" src/ProductList.jsx

2Fix

Call the hook once at the top. For one hook per list item, move the item into its own child component and call the hook there.

function Row({ item }) {
  const onPick = useCallback(() => select(item.id), [item.id]);
  return <li onClick={onPick}>{item.name}</li>;
}
// parent: items.map((item) => <Row key={item.id} item={item} />)

3Verify

Lint again and add or remove list items in the browser.

npx eslint src/ProductList.jsx

Confirmed by react.dev

Did this fix it?

How do I stop it coming back?

  • Keep eslint-plugin-react-hooks on in the editor and in CI so the error shows up while typing.
  • Write hooks as the first lines of every component, and put guards and early returns underneath them.
  • Express conditions inside the hook's callback or inside its body, never around the hook call.
  • When a loop needs hooks per item, make the item a child component.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Web apps

React says a component rendered fewer hooks than the last render

Uncaught Error: Rendered fewer hooks than expected. This may be caused by an accidental early return statement.

React matches hooks to state by call order, so every render of a component must call the same hooks in the same order.

2 causes, each with a fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.