Skip to content

Commit c21c41e

Browse files
authored
Tweak invalid Hook warning and error (#14747)
1 parent fec00a8 commit c21c41e

File tree

5 files changed

+22
-16
lines changed

5 files changed

+22
-16
lines changed

packages/react-dom/src/server/ReactPartialRendererHooks.js

+5-3
Original file line numberDiff line numberDiff line change
@@ -57,13 +57,15 @@ let currentHookNameInDev: ?string;
5757
function resolveCurrentlyRenderingComponent(): Object {
5858
invariant(
5959
currentlyRenderingComponent !== null,
60-
'Hooks can only be called inside the body of a function component.',
60+
'Hooks can only be called inside the body of a function component. ' +
61+
'(https://fb.me/react-invalid-hook-call)',
6162
);
6263
if (__DEV__) {
6364
warning(
6465
!isInHookUserCodeInDev,
65-
'Hooks can only be called inside the body of a function component. ' +
66-
'Do not call Hooks inside other Hooks. For more information, see ' +
66+
'Do not call Hooks inside useEffect(...), useMemo(...), or other built-in Hooks. ' +
67+
'You can only call Hooks at the top level of your React function. ' +
68+
'For more information, see ' +
6769
'https://fb.me/rules-of-hooks',
6870
);
6971
}

packages/react-reconciler/src/ReactFiberHooks.js

+5-3
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,8 @@ function warnOnHookMismatchInDev() {
235235
function throwInvalidHookError() {
236236
invariant(
237237
false,
238-
'Hooks can only be called inside the body of a function component.',
238+
'Hooks can only be called inside the body of a function component. ' +
239+
'(https://fb.me/react-invalid-hook-call)',
239240
);
240241
}
241242

@@ -1188,8 +1189,9 @@ if (__DEV__) {
11881189
const warnInvalidHookAccess = () => {
11891190
warning(
11901191
false,
1191-
'Hooks can only be called inside the body of a function component. ' +
1192-
'Do not call Hooks inside other Hooks. For more information, see ' +
1192+
'Do not call Hooks inside useEffect(...), useMemo(...), or other built-in Hooks. ' +
1193+
'You can only call Hooks at the top level of your React function. ' +
1194+
'For more information, see ' +
11931195
'https://fb.me/rules-of-hooks',
11941196
);
11951197
};

packages/react-reconciler/src/__tests__/ReactHooks-test.internal.js

+8-8
Original file line numberDiff line numberDiff line change
@@ -665,7 +665,7 @@ describe('ReactHooks', () => {
665665
return null;
666666
}
667667
expect(() => ReactTestRenderer.create(<App />)).toWarnDev(
668-
'Hooks can only be called inside the body of a function component',
668+
'Do not call Hooks inside useEffect(...), useMemo(...), or other built-in Hooks.',
669669
);
670670
});
671671

@@ -835,12 +835,12 @@ describe('ReactHooks', () => {
835835
if (__DEV__) {
836836
expect(console.error).toHaveBeenCalledTimes(3);
837837
expect(console.error.calls.argsFor(0)[0]).toContain(
838-
'Hooks can only be called inside the body of a function component',
838+
'Do not call Hooks inside useEffect(...), useMemo(...), or other built-in Hooks',
839839
);
840840
}
841841
});
842842

843-
it("throws when calling hooks inside useState's initialize function", () => {
843+
it("warns when calling hooks inside useState's initialize function", () => {
844844
const {useState, useRef} = React;
845845
function App() {
846846
useState(() => {
@@ -850,7 +850,7 @@ describe('ReactHooks', () => {
850850
return null;
851851
}
852852
expect(() => ReactTestRenderer.create(<App />)).toWarnDev(
853-
'Hooks can only be called inside the body of a function component',
853+
'Do not call Hooks inside useEffect(...), useMemo(...), or other built-in Hooks.',
854854
);
855855
});
856856

@@ -892,9 +892,9 @@ describe('ReactHooks', () => {
892892
}).toWarnDev([
893893
// We see it twice due to replay
894894
'Context can only be read while React is rendering',
895-
'Hooks can only be called inside the body of a function component',
895+
'Do not call Hooks inside useEffect(...), useMemo(...), or other built-in Hooks',
896896
'Context can only be read while React is rendering',
897-
'Hooks can only be called inside the body of a function component',
897+
'Do not call Hooks inside useEffect(...), useMemo(...), or other built-in Hooks',
898898
]);
899899

900900
function Valid() {
@@ -925,9 +925,9 @@ describe('ReactHooks', () => {
925925
}).toWarnDev([
926926
// We see it twice due to replay
927927
'Context can only be read while React is rendering',
928-
'Hooks can only be called inside the body of a function component',
928+
'Do not call Hooks inside useEffect(...), useMemo(...), or other built-in Hooks',
929929
'Context can only be read while React is rendering',
930-
'Hooks can only be called inside the body of a function component',
930+
'Do not call Hooks inside useEffect(...), useMemo(...), or other built-in Hooks',
931931
]);
932932
});
933933

packages/react-test-renderer/src/ReactShallowRenderer.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,8 @@ class ReactShallowRenderer {
218218
_validateCurrentlyRenderingComponent() {
219219
invariant(
220220
this._currentlyRenderingComponent !== null,
221-
'Hooks can only be called inside the body of a function component.',
221+
'Hooks can only be called inside the body of a function component. ' +
222+
'(https://fb.me/react-invalid-hook-call)',
222223
);
223224
}
224225

packages/react/src/ReactHooks.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ function resolveDispatcher() {
1717
const dispatcher = ReactCurrentDispatcher.current;
1818
invariant(
1919
dispatcher !== null,
20-
'Hooks can only be called inside the body of a function component.',
20+
'Hooks can only be called inside the body of a function component. ' +
21+
'(https://fb.me/react-invalid-hook-call)',
2122
);
2223
return dispatcher;
2324
}

0 commit comments

Comments
 (0)