Skip to content

Commit d0decb9

Browse files
committed
Split code examples
1 parent 25b51be commit d0decb9

File tree

1 file changed

+11
-10
lines changed

1 file changed

+11
-10
lines changed

content/warnings/invalid-hook-call-warning.md

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -29,15 +29,7 @@ You can only call Hooks **while React is rendering a function component**:
2929

3030
**Learn more about this in the [Rules of Hooks](/docs/hooks-rules.html).**
3131

32-
To avoid confusion, it’s **not** supported to call Hooks in other cases:
33-
34-
* 🔴 Do not call Hooks in class components.
35-
* 🔴 Do not call in event handlers.
36-
* 🔴 Do not call Hooks inside functions passed to `useMemo`, `useReducer`, or `useEffect`.
37-
38-
If you break these rules, you might see this error.
39-
40-
```js{2-3,8-9,15-16,23-24,33-34}
32+
```js{2-3,8-9}
4133
function Counter() {
4234
// ✅ Good: top-level in a function component
4335
const [count, setCount] = useState(0);
@@ -49,7 +41,17 @@ function useWindowWidth() {
4941
const [width, setWidth] = useState(window.innerWidth);
5042
// ...
5143
}
44+
```
5245

46+
To avoid confusion, it’s **not** supported to call Hooks in other cases:
47+
48+
* 🔴 Do not call Hooks in class components.
49+
* 🔴 Do not call in event handlers.
50+
* 🔴 Do not call Hooks inside functions passed to `useMemo`, `useReducer`, or `useEffect`.
51+
52+
If you break these rules, you might see this error.
53+
54+
```js{3-4,11-12,20-21}
5355
function Bad1() {
5456
function handleClick() {
5557
// 🔴 Bad: inside an event handler (to fix, move it outside!)
@@ -67,7 +69,6 @@ function Bad2() {
6769
// ...
6870
}
6971
70-
7172
class Bad3 extends React.Component {
7273
render() {
7374
// 🔴 Bad: inside a class component

0 commit comments

Comments
 (0)