@@ -47,6 +47,7 @@ import {
4747 enableUseDeferredValueInitialArg ,
4848 disableLegacyMode ,
4949 enableNoCloningMemoCache ,
50+ enableContextProfiling ,
5051} from 'shared/ReactFeatureFlags' ;
5152import {
5253 REACT_CONTEXT_TYPE ,
@@ -81,7 +82,11 @@ import {
8182 ContinuousEventPriority ,
8283 higherEventPriority ,
8384} from './ReactEventPriorities' ;
84- import { readContext , checkIfContextChanged } from './ReactFiberNewContext' ;
85+ import {
86+ readContext ,
87+ readContextAndCompare ,
88+ checkIfContextChanged ,
89+ } from './ReactFiberNewContext' ;
8590import { HostRoot , CacheComponent , HostComponent } from './ReactWorkTags' ;
8691import {
8792 LayoutStatic as LayoutStaticEffect ,
@@ -1053,6 +1058,13 @@ function updateWorkInProgressHook(): Hook {
10531058 return workInProgressHook ;
10541059}
10551060
1061+ function unstable_useContextWithBailout < T > (
1062+ context : ReactContext < T > ,
1063+ compare : void | ( T => mixed ) ,
1064+ ) : T {
1065+ return readContextAndCompare ( context , compare ) ;
1066+ }
1067+
10561068// NOTE: defining two versions of this function to avoid size impact when this feature is disabled.
10571069// Previously this function was inlined, the additional `memoCache` property makes it not inlined.
10581070let createFunctionComponentUpdateQueue : ( ) => FunctionComponentUpdateQueue ;
@@ -3689,6 +3701,10 @@ if (enableAsyncActions) {
36893701if (enableAsyncActions) {
36903702 ( ContextOnlyDispatcher : Dispatcher ) . useOptimistic = throwInvalidHookError ;
36913703}
3704+ if (enableContextProfiling) {
3705+ ( ContextOnlyDispatcher : Dispatcher ) . unstable_useContextWithBailout =
3706+ throwInvalidHookError ;
3707+ }
36923708
36933709const HooksDispatcherOnMount: Dispatcher = {
36943710 readContext ,
@@ -3728,6 +3744,10 @@ if (enableAsyncActions) {
37283744if (enableAsyncActions) {
37293745 ( HooksDispatcherOnMount : Dispatcher ) . useOptimistic = mountOptimistic ;
37303746}
3747+ if (enableContextProfiling) {
3748+ ( HooksDispatcherOnMount : Dispatcher ) . unstable_useContextWithBailout =
3749+ unstable_useContextWithBailout ;
3750+ }
37313751
37323752const HooksDispatcherOnUpdate: Dispatcher = {
37333753 readContext ,
@@ -3767,6 +3787,10 @@ if (enableAsyncActions) {
37673787if (enableAsyncActions) {
37683788 ( HooksDispatcherOnUpdate : Dispatcher ) . useOptimistic = updateOptimistic ;
37693789}
3790+ if (enableContextProfiling) {
3791+ ( HooksDispatcherOnUpdate : Dispatcher ) . unstable_useContextWithBailout =
3792+ unstable_useContextWithBailout ;
3793+ }
37703794
37713795const HooksDispatcherOnRerender: Dispatcher = {
37723796 readContext ,
@@ -3806,6 +3830,10 @@ if (enableAsyncActions) {
38063830if (enableAsyncActions) {
38073831 ( HooksDispatcherOnRerender : Dispatcher ) . useOptimistic = rerenderOptimistic ;
38083832}
3833+ if (enableContextProfiling) {
3834+ ( HooksDispatcherOnRerender : Dispatcher ) . unstable_useContextWithBailout =
3835+ unstable_useContextWithBailout ;
3836+ }
38093837
38103838let HooksDispatcherOnMountInDEV: Dispatcher | null = null;
38113839let HooksDispatcherOnMountWithHookTypesInDEV: Dispatcher | null = null;
@@ -4019,6 +4047,14 @@ if (__DEV__) {
40194047 return mountOptimistic ( passthrough , reducer ) ;
40204048 } ;
40214049 }
4050+ if ( enableContextProfiling ) {
4051+ ( HooksDispatcherOnMountInDEV : Dispatcher ) . unstable_useContextWithBailout =
4052+ function < T > (context: ReactContext< T > , compare: void | (T => mixed ) ) : T {
4053+ currentHookNameInDev = 'useContext' ;
4054+ mountHookTypesDev ( ) ;
4055+ return unstable_useContextWithBailout ( context , compare ) ;
4056+ } ;
4057+ }
40224058
40234059 HooksDispatcherOnMountWithHookTypesInDEV = {
40244060 readContext < T > (context: ReactContext< T > ): T {
@@ -4200,6 +4236,14 @@ if (__DEV__) {
42004236 return mountOptimistic ( passthrough , reducer ) ;
42014237 } ;
42024238 }
4239+ if ( enableContextProfiling ) {
4240+ ( HooksDispatcherOnMountWithHookTypesInDEV : Dispatcher ) . unstable_useContextWithBailout =
4241+ function < T > (context: ReactContext< T > , compare: void | (T => mixed ) ) : T {
4242+ currentHookNameInDev = 'useContext' ;
4243+ updateHookTypesDev ( ) ;
4244+ return unstable_useContextWithBailout ( context , compare ) ;
4245+ } ;
4246+ }
42034247
42044248 HooksDispatcherOnUpdateInDEV = {
42054249 readContext < T > (context: ReactContext< T > ): T {
@@ -4380,6 +4424,14 @@ if (__DEV__) {
43804424 return updateOptimistic ( passthrough , reducer ) ;
43814425 } ;
43824426 }
4427+ if ( enableContextProfiling ) {
4428+ ( HooksDispatcherOnUpdateInDEV : Dispatcher ) . unstable_useContextWithBailout =
4429+ function < T > (context: ReactContext< T > , compare: void | (T => mixed ) ) : T {
4430+ currentHookNameInDev = 'useContext' ;
4431+ updateHookTypesDev ( ) ;
4432+ return unstable_useContextWithBailout ( context , compare ) ;
4433+ } ;
4434+ }
43834435
43844436 HooksDispatcherOnRerenderInDEV = {
43854437 readContext < T > (context: ReactContext< T > ): T {
@@ -4560,6 +4612,14 @@ if (__DEV__) {
45604612 return rerenderOptimistic ( passthrough , reducer ) ;
45614613 } ;
45624614 }
4615+ if ( enableContextProfiling ) {
4616+ ( HooksDispatcherOnUpdateInDEV : Dispatcher ) . unstable_useContextWithBailout =
4617+ function < T > (context: ReactContext< T > , compare: void | (T => mixed ) ) : T {
4618+ currentHookNameInDev = 'useContext' ;
4619+ updateHookTypesDev ( ) ;
4620+ return unstable_useContextWithBailout ( context , compare ) ;
4621+ } ;
4622+ }
45634623
45644624 InvalidNestedHooksDispatcherOnMountInDEV = {
45654625 readContext < T > (context: ReactContext< T > ): T {
@@ -4766,6 +4826,15 @@ if (__DEV__) {
47664826 return mountOptimistic ( passthrough , reducer ) ;
47674827 } ;
47684828 }
4829+ if ( enableContextProfiling ) {
4830+ ( HooksDispatcherOnUpdateInDEV : Dispatcher ) . unstable_useContextWithBailout =
4831+ function < T > (context: ReactContext< T > , compare: void | (T => mixed ) ) : T {
4832+ currentHookNameInDev = 'useContext' ;
4833+ warnInvalidHookAccess ( ) ;
4834+ mountHookTypesDev ( ) ;
4835+ return unstable_useContextWithBailout ( context , compare ) ;
4836+ } ;
4837+ }
47694838
47704839 InvalidNestedHooksDispatcherOnUpdateInDEV = {
47714840 readContext < T > (context: ReactContext< T > ): T {
@@ -4972,6 +5041,15 @@ if (__DEV__) {
49725041 return updateOptimistic ( passthrough , reducer ) ;
49735042 } ;
49745043 }
5044+ if ( enableContextProfiling ) {
5045+ ( InvalidNestedHooksDispatcherOnUpdateInDEV : Dispatcher ) . unstable_useContextWithBailout =
5046+ function < T > (context: ReactContext< T > , compare: void | (T => mixed ) ) : T {
5047+ currentHookNameInDev = 'useContext' ;
5048+ warnInvalidHookAccess ( ) ;
5049+ updateHookTypesDev ( ) ;
5050+ return unstable_useContextWithBailout ( context , compare ) ;
5051+ } ;
5052+ }
49755053
49765054 InvalidNestedHooksDispatcherOnRerenderInDEV = {
49775055 readContext < T > (context: ReactContext< T > ): T {
@@ -5178,4 +5256,13 @@ if (__DEV__) {
51785256 return rerenderOptimistic ( passthrough , reducer ) ;
51795257 } ;
51805258 }
5259+ if ( enableContextProfiling ) {
5260+ ( InvalidNestedHooksDispatcherOnRerenderInDEV : Dispatcher ) . unstable_useContextWithBailout =
5261+ function < T > (context: ReactContext< T > , compare: void | (T => mixed ) ) : T {
5262+ currentHookNameInDev = 'useContext' ;
5263+ warnInvalidHookAccess ( ) ;
5264+ updateHookTypesDev ( ) ;
5265+ return unstable_useContextWithBailout ( context , compare ) ;
5266+ } ;
5267+ }
51815268}
0 commit comments