Skip to content

Commit 58e8304

Browse files
authoredJan 19, 2021
Remove custom error message from hook access error (#20604)
It will still result in a null access error, so there's no change in semantics. We will print a user-friendly error message in DEV.
1 parent 9043626 commit 58e8304

File tree

3 files changed

+28
-15
lines changed

3 files changed

+28
-15
lines changed
 

‎packages/react-debug-tools/src/__tests__/ReactHooksInspection-test.js

+5-2
Original file line numberDiff line numberDiff line change
@@ -277,14 +277,17 @@ describe('ReactHooksInspection', () => {
277277
};
278278

279279
expect(() => {
280-
ReactDebugTools.inspectHooks(Foo, {}, FakeDispatcherRef);
281-
}).toThrow(
280+
expect(() => {
281+
ReactDebugTools.inspectHooks(Foo, {}, FakeDispatcherRef);
282+
}).toThrow("Cannot read property 'useState' of null");
283+
}).toErrorDev(
282284
'Invalid hook call. Hooks can only be called inside of the body of a function component. This could happen for' +
283285
' one of the following reasons:\n' +
284286
'1. You might have mismatching versions of React and the renderer (such as React DOM)\n' +
285287
'2. You might be breaking the Rules of Hooks\n' +
286288
'3. You might have more than one copy of React in the same app\n' +
287289
'See https://reactjs.org/link/invalid-hook-call for tips about how to debug and fix this problem.',
290+
{withoutStack: true},
288291
);
289292

290293
expect(getterCalls).toBe(1);

‎packages/react-reconciler/src/__tests__/ReactHooksWithNoopRenderer-test.js

+6-1
Original file line numberDiff line numberDiff line change
@@ -249,13 +249,18 @@ describe('ReactHooksWithNoopRenderer', () => {
249249
}
250250

251251
it('throws when called outside the render phase', () => {
252-
expect(() => useState(0)).toThrow(
252+
expect(() => {
253+
expect(() => useState(0)).toThrow(
254+
"Cannot read property 'useState' of null",
255+
);
256+
}).toErrorDev(
253257
'Invalid hook call. Hooks can only be called inside of the body of a function component. This could happen for' +
254258
' one of the following reasons:\n' +
255259
'1. You might have mismatching versions of React and the renderer (such as React DOM)\n' +
256260
'2. You might be breaking the Rules of Hooks\n' +
257261
'3. You might have more than one copy of React in the same app\n' +
258262
'See https://reactjs.org/link/invalid-hook-call for tips about how to debug and fix this problem.',
263+
{withoutStack: true},
259264
);
260265
});
261266

‎packages/react/src/ReactHooks.js

+17-12
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
* @flow
88
*/
99

10+
import type {Dispatcher} from 'react-reconciler/src/ReactInternalTypes';
1011
import type {
1112
MutableSource,
1213
MutableSourceGetSnapshotFn,
@@ -15,25 +16,29 @@ import type {
1516
} from 'shared/ReactTypes';
1617
import type {OpaqueIDType} from 'react-reconciler/src/ReactFiberHostConfig';
1718

18-
import invariant from 'shared/invariant';
19-
2019
import ReactCurrentDispatcher from './ReactCurrentDispatcher';
2120

2221
type BasicStateAction<S> = (S => S) | S;
2322
type Dispatch<A> = A => void;
2423

2524
function resolveDispatcher() {
2625
const dispatcher = ReactCurrentDispatcher.current;
27-
invariant(
28-
dispatcher !== null,
29-
'Invalid hook call. Hooks can only be called inside of the body of a function component. This could happen for' +
30-
' one of the following reasons:\n' +
31-
'1. You might have mismatching versions of React and the renderer (such as React DOM)\n' +
32-
'2. You might be breaking the Rules of Hooks\n' +
33-
'3. You might have more than one copy of React in the same app\n' +
34-
'See https://reactjs.org/link/invalid-hook-call for tips about how to debug and fix this problem.',
35-
);
36-
return dispatcher;
26+
if (__DEV__) {
27+
if (dispatcher === null) {
28+
console.error(
29+
'Invalid hook call. Hooks can only be called inside of the body of a function component. This could happen for' +
30+
' one of the following reasons:\n' +
31+
'1. You might have mismatching versions of React and the renderer (such as React DOM)\n' +
32+
'2. You might be breaking the Rules of Hooks\n' +
33+
'3. You might have more than one copy of React in the same app\n' +
34+
'See https://reactjs.org/link/invalid-hook-call for tips about how to debug and fix this problem.',
35+
);
36+
}
37+
}
38+
// Will result in a null access error if accessed outside render phase. We
39+
// intentionally don't throw our own error because this is in a hot path.
40+
// Also helps ensure this is inlined.
41+
return ((dispatcher: any): Dispatcher);
3742
}
3843

3944
export function getCacheForType<T>(resourceType: () => T): T {

0 commit comments

Comments
 (0)