Maaz Zindani

Loading the index…

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

React says a Hook was called where React is not rendering a component

ErrorInvalid 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.

  • Applies to React 18, React 19, react-dom 18 and 19, npm 10, Vite 6 and 7
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 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 react
  2. 2Match what you see, then jump to its fix

    1. 1The 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. 2The 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. 3npm 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. 4npm 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

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 src

2Fix

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 errors

Confirmed by react.dev

Did this fix it?

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" src

2Fix

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=false

Confirmed by react.dev

Did this fix it?

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 6

2Fix

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/react

3Verify

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

Did this fix it?

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-native

2Fix

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 install

3Verify

react and react-dom now show the same version.

npm ls react react-dom

Confirmed by react.dev

Did this fix it?

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:

Related fixes

Next step

Want this handled for good?

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