React says a Hook was called where React is not rendering a component
Invalid hook call. Hooks can only be called inside of the body of a function component. This could happen for one of the following reasons:Short answer
A Hook only works while React is rendering a function component or a custom Hook. The error means a Hook ran somewhere else, or ran inside a copy of React that is not the one rendering. The usual causes are a conditional or nested Hook call, a component called like a function, two copies of react in node_modules, or react and react-dom at different versions.
Fix it fast
1Find out which cause you have
Two quick checks separate the causes. The first lists every copy of react and react-dom that the package manager installed: more than one path for react, or react and react-dom at different versions, is a dependency problem. If each is a single copy at one version, the cause is in the code, and the stack trace under the error names the component that called the Hook.
npm ls react react-dom npm explain react2Match what you see, then jump to its fix
- 1
The stack trace names one component, and the same component also reports 'Rendered more hooks than during the previous render' or runs fine until a prop changes.A Hook is called conditionally, in a loop, after an early return, or inside another functionFix - 2
The failing line is inside a helper whose name does not start with use, a class component, a module-level statement, or code that writes Article() instead of <Article />.The Hook runs outside rendering: a plain function, a class, or a component called like a functionFix - 3
npm ls react shows two different paths, often a library with its own node_modules/react or a linked package. The error says reading 'useState' of null, and the hook code itself is correct.There are two copies of React in node_modulesFix - 4
npm ls shows react and react-dom at different versions, or react-dom older than 16.8.0, or react-native older than 0.59.react and react-dom are at different versions, or too old for HooksFix
- 1
What does the error look like?
Invalid hook call. Hooks can only be called inside of the body of a function component. This could happen for one of the following reasons:Uncaught TypeError: Cannot read properties of null (reading 'useState')Uncaught TypeError: Cannot read properties of null (reading 'useContext')Rendered more hooks than during the previous render.
Cause 1 of 4
A Hook is called conditionally, in a loop, after an early return, or inside another function
1Check
The React docs say a Hook must be called at the top level of a function component or custom Hook: not inside conditions, loops, nested functions or try, catch and finally blocks, and not after a return. They add that the same restriction covers functions passed to useMemo, useReducer and useEffect. The ESLint plugin finds these automatically.
npm install --save-dev eslint-plugin-react-hooks
# eslint.config.js: add the plugin and the rule, then run npx eslint src
# plugins: { 'react-hooks': reactHooks },
# rules: { 'react-hooks/rules-of-hooks': 'error', 'react-hooks/exhaustive-deps': 'warn' }
npx eslint src2Fix
Call the Hook at the top, unconditionally, and put the condition inside it or after it. For a Hook needed inside an event handler, call it at the top of the component and use its result in the handler.
// before
function Panel({ open }) {
if (!open) return null
const theme = useContext(ThemeContext) // after an early return
return <div className={theme.panel} />
}
// after
function Panel({ open }) {
const theme = useContext(ThemeContext)
if (!open) return null
return <div className={theme.panel} />
}3Verify
Lint reports no rules-of-hooks error and the component renders with changing props.
npx eslint src # no react-hooks/rules-of-hooks errorsConfirmed by react.dev
Cause 2 of 4
The Hook runs outside rendering: a plain function, a class, or a component called like a function
1Check
React only allows Hooks from function components and custom Hooks. Its rules also say components must be used in JSX and never called as regular functions, because a direct call runs the component's Hooks inside the caller instead of in its own component.
# a call of a PascalCase component as a function
grep -rnE "\{[A-Z][A-Za-z0-9]*\(" src --include=*.jsx --include=*.tsx
# Hooks used in class bodies
grep -rnE "extends (React\.)?Component" src2Fix
Render components with JSX. Move Hook calls into a function component, or into a custom Hook whose name begins with use and which is itself called only from components. A class component cannot use Hooks: convert it to a function, or wrap the Hook in a small function component and render that inside the class.
// bad: runs Article's Hooks inside BlogPost
function BlogPost() { return <Layout>{Article()}</Layout> }
// good
function BlogPost() { return <Layout><Article /></Layout> }
// helper that needs a Hook becomes a custom Hook
function useWindowWidth() {
const [width, setWidth] = useState(window.innerWidth)
useEffect(() => {
const onResize = () => setWidth(window.innerWidth)
window.addEventListener('resize', onResize)
return () => window.removeEventListener('resize', onResize)
}, [])
return width
}3Verify
The component renders without the error and its state persists across renders.
npm test -- --watchAll=falseConfirmed by react.dev
Cause 3 of 4
There are two copies of React in node_modules
1Check
Hooks need the react your code imports to be the same module as the react that react-dom uses. The React docs list a library that has react as a regular dependency instead of a peer dependency, and npm link, as common reasons for two copies. Find where the second copy is and who requires it.
npm ls react
npm explain react
find node_modules -type d -path '*/node_modules/react' -maxdepth 62Fix
Make the tree contain one copy. Run npm dedupe first. Libraries that you publish should list react and react-dom in peerDependencies, not dependencies. In Vite, tell the resolver to use one copy for linked packages or monorepos. For an npm-linked library, link the app's React into the library instead of installing another one.
npm dedupe
// vite.config.js
export default defineConfig({
resolve: { dedupe: ['react', 'react-dom'] },
})
# library under development, run from the library folder
npm link ../myapp/node_modules/react3Verify
Only one react path remains, and two reads of require('react') are the same module.
npm ls react
# React's own debugging check: put this in a component file after temporarily adding
# window.React1 = require('react') to node_modules/react-dom/index.js
# require('react-dom'); window.React2 = require('react'); console.log(window.React1 === window.React2)Confirmed by react.dev
Cause 4 of 4
react and react-dom are at different versions, or too old for Hooks
1Check
Hooks arrived in React 16.8, and the React docs name an old react-dom or react-native as one cause. Packages that render (react-dom, react-native) must be on a Hooks-capable release and installed beside the same react.
npm ls react react-dom
npm ls react-native2Fix
Install matching, current versions of both packages together, then reinstall to clear stale copies. If a library pins an older react-dom in its own dependencies, update it or override it so the tree has one version.
npm install react@latest react-dom@latest
rm -rf node_modules && npm install3Verify
react and react-dom now show the same version.
npm ls react react-domConfirmed by react.dev
How do I stop it coming back?
- Turn on eslint-plugin-react-hooks and make rules-of-hooks an error in CI.
- Put react and react-dom in peerDependencies of every library you publish.
- Install react and react-dom together and upgrade them in one command.
- Run npm ls react in CI and fail when more than one copy is found.
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.
- TaskvareA task and project-management suite: React on the web, Flutter on mobile, TypeScript underneath.
Related fixes
Web apps
React was asked to render something that is not a component (it got undefined)
You likely forgot to export your component from the file it's defined in, or you might have mixed up default and named imports.
React received something that is not a tag name or a component, most often undefined, because the import resolved to nothing.
4 causes, each with a fixWeb apps
Module not found for antd/dist/antd.less after upgrading to Ant Design 5
Module not found: Error: Can't resolve 'antd/dist/antd.less'
Ant Design 5 dropped Less and the bundled CSS file for CSS-in-JS, so antd/dist/antd.less no longer exists.
3 causes, each with a fixWeb apps
Astro Code component fails inside a React component
Astro components cannot be rendered directly via function call
Astro components such as Code only exist on the server and render static HTML.
2 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
How to catch and handle a 422 from Axios in a Redux thunk
action.error.message is 'Rejected' and action.payload is undefined
A 422 means the server read the request but cannot process its content, so repeating it unchanged fails again.
4 causes, each with a fixWeb apps
axios gets 429 Too Many Requests from a React app
429 (Too Many Requests)
A 429 comes from the server or an API gateway, not from axios: your client sent too many requests in a window.
4 causes, each with a fix