Last updated: Apr 6, 2024
Reading timeยท4 min
The error "Invalid hook call. Hooks can only be called inside the body of a function component" occurs for multiple reasons:
react
and react-dom
.react
package in the same project.App()
instead of <App />
react
and react-dom
Open your terminal in your project's root directory and
update the versions of your react
and react-dom
packages
to make sure the versions match and you aren't using an outdated version.
# ๐๏ธ with NPM npm install react@latest react-dom@latest # ๐๏ธ ONLY If you use TypeScript npm install --save-dev @types/react@latest @types/react-dom@latest # ---------------------------------------------- # ๐๏ธ with YARN yarn add react@latest react-dom@latest # ๐๏ธ ONLY If you use TypeScript yarn add @types/react@latest @types/react-dom@latest --dev
If the error persists, try to delete your node_modules
and
package-lock.json (not
package.json
) files, re-run npm install
and restart your IDE.
The error is often caused by having multiple versions of react
in the same
project.
If you are on macOS or Linux, issue the following commands in bash
or zsh
.
# for macOS and Linux rm -rf node_modules rm -f package-lock.json rm -f yarn.lock # ๐๏ธ clean your npm cache npm cache clean --force # ๐๏ธ install packages npm install
If you are on Windows, issue the following commands in CMD.
# for Windows rd /s /q "node_modules" del package-lock.json del -f yarn.lock # ๐๏ธ clean your npm cache npm cache clean --force # ๐๏ธ install packages npm install
Make sure to restart your IDE and dev server if the error persists. VSCode often glitches and needs a reboot.
Here is another example of how the error occurs.
import {useState} from 'react'; // ๐๏ธ Don't call components as functions ๐๏ธ App(); export default function App() { /** * โ๏ธ Warning: 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: 1. You might have mismatching versions of React and the renderer (such as React DOM) 2. You might be breaking the Rules of Hooks 3. You might have more than one copy of React in the same app */ const [count, setCount] = useState(0); return ( <div> <h1>Hello world</h1> </div> ); }
App
component as a function.You should only use components with the JSX syntax, e.g.
<App country="Austria" age="30" />
, and not
App({country: 'Austria', age: 30})
.
If you have a class, convert it to a function to be able to use hooks.
Here is an example of how the error is caused in a function that is neither a component nor a custom hook.
import {useState, useEffect} from 'react'; // ๐๏ธ Not a component nor custom hook โ๏ธโ๏ธโ๏ธ // so it can't use hooks function counter() { const [count, setCount] = useState(0); useEffect(() => { console.log('hello world'); }, []); }
The counter
function starts with a lowercase c
, so it's not considered a
component by React because all
component names must start with a capital letter.
It also isn't a custom hook because its name doesn't start with use
, e.g.
useCounter
.
We are only able to use hooks inside of function components or custom hooks,
so one way to be able to use hooks is to rename counter
to useCounter
.
import {useState, useEffect} from 'react'; function useCounter() { const [count, setCount] = useState(0); useEffect(() => { console.log('hello world'); }, []); }
React now recognizes useCounter
as a custom hook, so it allows us to use hooks
inside of it.
Like the documentation states:
To solve the error "Invalid hook call. Hooks can only be called inside the body of a function component", make sure:
react
and react-dom
.react
installed.App()
instead of
<App />
.