@@ -29,15 +29,7 @@ You can only call Hooks **while React is rendering a function component**:
29
29
30
30
** Learn more about this in the [ Rules of Hooks] ( /docs/hooks-rules.html ) .**
31
31
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}
41
33
function Counter() {
42
34
// ✅ Good: top-level in a function component
43
35
const [count, setCount] = useState(0);
@@ -49,7 +41,17 @@ function useWindowWidth() {
49
41
const [width, setWidth] = useState(window.innerWidth);
50
42
// ...
51
43
}
44
+ ```
52
45
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}
53
55
function Bad1() {
54
56
function handleClick() {
55
57
// 🔴 Bad: inside an event handler (to fix, move it outside!)
@@ -67,7 +69,6 @@ function Bad2() {
67
69
// ...
68
70
}
69
71
70
-
71
72
class Bad3 extends React.Component {
72
73
render() {
73
74
// 🔴 Bad: inside a class component
0 commit comments