GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Remove custom error message from hook access error (#20604) · react/react@58e8304

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • packages
    • react-debug-tools/src/__tests__
      • ReactHooksInspection-test.js
    • react-reconciler/src/__tests__
      • ReactHooksWithNoopRenderer-test.js
    • react/src
      • ReactHooks.js
Expand file treeCollapse file tree
Open diff view settings
Collapse file

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

Copy file name to clipboardExpand all lines: packages/react-debug-tools/src/__tests__/ReactHooksInspection-test.js
+5-2Lines changed: 5 additions & 2 deletions
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);
Collapse file

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

Copy file name to clipboardExpand all lines: packages/react-reconciler/src/__tests__/ReactHooksWithNoopRenderer-test.js
+6-1Lines changed: 6 additions & 1 deletion
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

Collapse file

‎packages/react/src/ReactHooks.js‎

Copy file name to clipboardExpand all lines: packages/react/src/ReactHooks.js
+17-12Lines changed: 17 additions & 12 deletions
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 (0)
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.