-
Notifications
You must be signed in to change notification settings - Fork 7.9k
[errors] Add more info to "Invalid Hook Call" error page #8293
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
rickhanlonii
wants to merge
3
commits into
reactjs:main
Choose a base branch
from
rickhanlonii:rh/377
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+182
−8
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,127 @@ | ||
| <Intro> | ||
|
|
||
| This page explains this React error and common ways to fix it. | ||
|
|
||
| </Intro> | ||
|
|
||
| The full text of the error is: | ||
|
|
||
| <ErrorDecoder /> | ||
|
|
||
| <Note> | ||
|
|
||
| In the minified production build of React, full error messages are replaced with short codes to reduce bundle size. We recommend using the development build when debugging, as it includes additional warnings and debug information. | ||
|
|
||
| </Note> | ||
|
|
||
| ## What This Error Means {/*what-this-error-means*/} | ||
|
|
||
| This error occurs when a Hook is called in a way that violates the [Rules of Hooks](/reference/rules/rules-of-hooks): | ||
|
|
||
| ```js {4} | ||
| export default function Counter() { | ||
| function handleClick() { | ||
| // 🔴 Invalid Hook call! | ||
| const [count, setCount] = useState(0); | ||
| setCount(count + 1); | ||
| } | ||
|
|
||
| return <button onClick={handleClick}>Click me</button>; | ||
| } | ||
| ``` | ||
|
|
||
| You can only call Hooks at the top level of a function component or a custom Hook. React tracks Hooks by associating them with the component that is currently rendering. When you call a Hook and no component is rendering, React cannot associate the Hook with a component, and throws this error. | ||
|
|
||
| The most common cause is calling a Hook outside a function component. For example, inside an event handler, a class component, or a regular function. Another common cause is having **multiple copies of React** loaded in your app, which is a build configuration problem, not a coding mistake. | ||
|
|
||
| [See the examples below for common causes and how to fix them.](#common-causes) | ||
|
|
||
| ## Common Causes {/*common-causes*/} | ||
|
|
||
| ### Calling a Hook outside the body of a function component {/*calling-a-hook-outside-the-body-of-a-function-component*/} | ||
|
|
||
| React requires you to call Hooks at the top level of a function component or a custom Hook—not inside event handlers, nested functions, or class components. | ||
|
|
||
| Here is an example of code that would trigger this error: | ||
|
|
||
| <Sandpack> | ||
|
|
||
| ```js {expectedErrors: {'react-compiler': [7]}} | ||
| import { useState } from 'react'; | ||
|
|
||
| export default function Counter() { | ||
| function handleClick() { | ||
| // 🔴 useState is called inside an event handler, not the component body | ||
| // eslint-disable-next-line react-hooks/rules-of-hooks | ||
| const [count, setCount] = useState(0); | ||
| setCount(count + 1); | ||
| } | ||
|
|
||
| return <button onClick={handleClick}>Click me</button>; | ||
| } | ||
| ``` | ||
|
|
||
| </Sandpack> | ||
|
|
||
| To fix this, move the Hook call to the top level of your component: | ||
|
|
||
| <Sandpack> | ||
|
|
||
| ```js | ||
| import { useState } from 'react'; | ||
|
|
||
| // ✅ Fixed: useState is called at the top level of the component | ||
| export default function Counter() { | ||
| const [count, setCount] = useState(0); | ||
|
|
||
| function handleClick() { | ||
| setCount(count + 1); | ||
| } | ||
|
|
||
| return <button onClick={handleClick}>Count: {count}</button>; | ||
| } | ||
| ``` | ||
|
|
||
| </Sandpack> | ||
|
|
||
| The same rule applies to class components—you cannot use Hooks in class components. If you need state or other React features in a class component, [convert it to a function component](/reference/react/Component#alternatives). | ||
|
|
||
| ### Multiple copies of React in your app {/*multiple-copies-of-react*/} | ||
|
|
||
| If your project uses a monorepo, `npm link`, or a third-party package that bundles its own copy of React, you can end up with two separate copies of React loaded at the same time. When this happens, the copy of React that your component uses is different from the copy that `react-dom` uses, and Hooks break because React can't track them across copies. | ||
|
|
||
| To check if this is your problem, run the following from your project root: | ||
|
|
||
| ```bash | ||
| npm ls react | ||
| ``` | ||
|
|
||
| If you see more than one entry for `react`, you have duplicate copies. You can also add a temporary log to confirm. Add this at the top of your component file: | ||
|
|
||
| ```js | ||
| import React from 'react'; | ||
| console.log(React === window.React); // false means duplicates | ||
| ``` | ||
|
|
||
| If you're using webpack, you can fix this by adding a resolve alias in your webpack config: | ||
|
|
||
| ```js | ||
| // webpack.config.js | ||
| module.exports = { | ||
| resolve: { | ||
| alias: { | ||
| react: require.resolve('react'), | ||
| 'react-dom': require.resolve('react-dom'), | ||
| }, | ||
| }, | ||
| }; | ||
| ``` | ||
|
|
||
| If you're using Vite, add a similar alias in your Vite config. For other bundlers, consult their documentation on how to configure module aliases. | ||
|
|
||
| ## Related Documentation {/*related-documentation*/} | ||
|
|
||
| - [Rules of Hooks](/reference/rules/rules-of-hooks) | ||
| - [`useState`](/reference/react/useState) | ||
| - [Your First Component](/learn/your-first-component) | ||
| - [Reusing Logic with Custom Hooks](/learn/reusing-logic-with-custom-hooks) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think here we need a caveat, or further instruction, as in, you first need to identify where a different React version might be used. And assign that React version to
window.React.