ESLint says a React Hook is called conditionally, in useCallback or any other hook
React 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.
Fix it fast
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.jsx2Match what you see, then jump to its fix
- 1
Did you accidentally call a React Hook after an early return?The hook sits below an early returnFix - 2
is 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
is 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
cannot 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
- 1
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.jsx2Fix
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.jsxConfirmed by react.dev
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.jsx2Fix
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.jsxConfirmed by react.dev
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.jsx2Fix
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.jsxConfirmed by github.com
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.jsx2Fix
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.jsxConfirmed by react.dev
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:
- TaxiDexMy own multi-tenant taxi dispatch SaaS: API, dispatcher portal, driver and passenger apps, and the servers underneath.
- QistLockA NestJS and React SaaS driving an Android Device Owner agent that locks financed phones when payments lapse.
- WorkforceA field app, an admin console and a self-hosted Node.js server for push and reconciliation.
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 fixWeb apps
React says a Hook was called where React is not rendering a component
Uncaught TypeError: Cannot read properties of null (reading 'useState')
A Hook only works while React is rendering a function component or a custom Hook.
4 causes, each with a fixWeb apps
React shows Attempted import error when importing a component or function
Attempted import error:
The build found the file but not the name you asked for.
2 causes, each with a fixWeb apps
AxiosError Network Error with code ERR_NETWORK in a React app
Network Error
ERR_NETWORK means the request produced no readable response.
4 causes, each with a fixWeb apps
Axios get works with a URL string but ignores the object passed as the second argument
The server log shows a GET with no query string
axios.get takes a URL and an optional config object.
3 causes, each with a fixWeb apps
Can't resolve '../axios' in a React project
Module not found: Error: Can't resolve '../axios'
A path that starts with ./ or ../ is a file path, not the axios package.
3 causes, each with a fix