7
7
* @flow
8
8
*/
9
9
10
+ import type { Dispatcher } from 'react-reconciler/src/ReactInternalTypes' ;
10
11
import type {
11
12
MutableSource ,
12
13
MutableSourceGetSnapshotFn ,
@@ -15,25 +16,29 @@ import type {
15
16
} from 'shared/ReactTypes' ;
16
17
import type { OpaqueIDType } from 'react-reconciler/src/ReactFiberHostConfig' ;
17
18
18
- import invariant from 'shared/invariant' ;
19
-
20
19
import ReactCurrentDispatcher from './ReactCurrentDispatcher' ;
21
20
22
21
type BasicStateAction < S > = ( S => S ) | S ;
23
22
type Dispatch < A > = A => void ;
24
23
25
24
function resolveDispatcher ( ) {
26
25
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 ) ;
37
42
}
38
43
39
44
export function getCacheForType < T > ( resourceType : ( ) = > T ) : T {
0 commit comments