@@ -2,7 +2,7 @@ import { Dispatch } from 'react/src/currentDispatcher';
22import { Dispatcher } from 'react/src/currentDispatcher' ;
33import currentBatchConfig from 'react/src/currentBatchConfig' ;
44import internals from 'shared/internals' ;
5- import { Action } from 'shared/ReactTypes' ;
5+ import { Action , ReactContext } from 'shared/ReactTypes' ;
66import { FiberNode } from './fiber' ;
77import { Flags , PassiveEffect } from './fiberFlags' ;
88import { Lane , NoLane , requestUpdateLane } from './fiberLanes' ;
@@ -82,14 +82,16 @@ const HooksDispatcherOnMount: Dispatcher = {
8282 useState : mountState ,
8383 useEffect : mountEffect ,
8484 useTransition : mountTransition ,
85- useRef : mountRef
85+ useRef : mountRef ,
86+ useContext : readContext
8687} ;
8788
8889const HooksDispatcherOnUpdate : Dispatcher = {
8990 useState : updateState ,
9091 useEffect : updateEffect ,
9192 useTransition : updateTransition ,
92- useRef : updateRef
93+ useRef : updateRef ,
94+ useContext : readContext
9395} ;
9496
9597function mountEffect ( create : EffectCallback | void , deps : EffectDeps | void ) {
@@ -379,3 +381,12 @@ function mountWorkInProgressHook(): Hook {
379381 }
380382 return workInProgressHook ;
381383}
384+
385+ function readContext < T > ( context : ReactContext < T > ) : T {
386+ const consumer = currentlyRenderingFiber ;
387+ if ( consumer === null ) {
388+ throw new Error ( '只能在函数组件中调用useContext' ) ;
389+ }
390+ const value = context . _currentValue ;
391+ return value ;
392+ }
0 commit comments