Skip to content

Commit 7ddd91f

Browse files
committed
claude you suck ass at git for real my guy
1 parent f233ff7 commit 7ddd91f

File tree

2 files changed

+131
-8
lines changed

2 files changed

+131
-8
lines changed

src/content/errors/321.md

Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
<Intro>
2+
3+
This page explains this React error and common ways to fix it.
4+
5+
</Intro>
6+
7+
The full text of the error is:
8+
9+
<ErrorDecoder />
10+
11+
<Note>
12+
13+
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.
14+
15+
</Note>
16+
17+
## What This Error Means {/*what-this-error-means*/}
18+
19+
This error occurs when a Hook is called in a way that violates the [Rules of Hooks](/reference/rules/rules-of-hooks):
20+
21+
```js {4}
22+
export default function Counter() {
23+
function handleClick() {
24+
// 🔴 Invalid Hook call!
25+
const [count, setCount] = useState(0);
26+
setCount(count + 1);
27+
}
28+
29+
return <button onClick={handleClick}>Click me</button>;
30+
}
31+
```
32+
33+
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.
34+
35+
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.
36+
37+
[See the examples below for common causes and how to fix them.](#common-causes)
38+
39+
## Common Causes {/*common-causes*/}
40+
41+
### Calling a Hook outside the body of a function component {/*calling-a-hook-outside-the-body-of-a-function-component*/}
42+
43+
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.
44+
45+
Here is an example of code that would trigger this error:
46+
47+
<Sandpack>
48+
49+
```js {expectedErrors: {'react-compiler': [7]}}
50+
import { useState } from 'react';
51+
52+
export default function Counter() {
53+
function handleClick() {
54+
// 🔴 useState is called inside an event handler, not the component body
55+
// eslint-disable-next-line react-hooks/rules-of-hooks
56+
const [count, setCount] = useState(0);
57+
setCount(count + 1);
58+
}
59+
60+
return <button onClick={handleClick}>Click me</button>;
61+
}
62+
```
63+
64+
</Sandpack>
65+
66+
To fix this, move the Hook call to the top level of your component:
67+
68+
<Sandpack>
69+
70+
```js
71+
import { useState } from 'react';
72+
73+
// ✅ Fixed: useState is called at the top level of the component
74+
export default function Counter() {
75+
const [count, setCount] = useState(0);
76+
77+
function handleClick() {
78+
setCount(count + 1);
79+
}
80+
81+
return <button onClick={handleClick}>Count: {count}</button>;
82+
}
83+
```
84+
85+
</Sandpack>
86+
87+
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).
88+
89+
### Multiple copies of React in your app {/*multiple-copies-of-react*/}
90+
91+
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.
92+
93+
To check if this is your problem, run the following from your project root:
94+
95+
```bash
96+
npm ls react
97+
```
98+
99+
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:
100+
101+
```js
102+
import React from 'react';
103+
console.log(React === window.React); // false means duplicates
104+
```
105+
106+
If you're using webpack, you can fix this by adding a resolve alias in your webpack config:
107+
108+
```js
109+
// webpack.config.js
110+
module.exports = {
111+
resolve: {
112+
alias: {
113+
react: require.resolve('react'),
114+
'react-dom': require.resolve('react-dom'),
115+
},
116+
},
117+
};
118+
```
119+
120+
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.
121+
122+
## Related Documentation {/*related-documentation*/}
123+
124+
- [Rules of Hooks](/reference/rules/rules-of-hooks)
125+
- [`useState`](/reference/react/useState)
126+
- [Your First Component](/learn/your-first-component)
127+
- [Reusing Logic with Custom Hooks](/learn/reusing-logic-with-custom-hooks)

src/content/errors/377.md

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,13 @@
11
<Intro>
22

3-
This page explains this React error and common ways to fix it.
3+
In the minified production build of React, we avoid sending down full error messages in order to reduce the number of bytes sent over the wire.
44

55
</Intro>
66

7-
The full text of the error is:
7+
We highly recommend using the development build locally when debugging your app since it tracks additional debug info and provides helpful warnings about potential problems in your apps, but if you encounter an exception while using the production build, this page will reassemble the original error message.
88

9-
<ErrorDecoder />
10-
11-
<Note>
9+
The full text of the error you just encountered is:
1210

13-
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.
14-
15-
</Note>
11+
<ErrorDecoder />
1612

1713
This error occurs when you pass a BigInt value from a Server Component to a Client Component.

0 commit comments

Comments
 (0)