diff --git a/packages/react-reconciler/src/ReactFiberNewContext.js b/packages/react-reconciler/src/ReactFiberNewContext.js index bf28da19ce046..2e8d01e99e89c 100644 --- a/packages/react-reconciler/src/ReactFiberNewContext.js +++ b/packages/react-reconciler/src/ReactFiberNewContext.js @@ -74,7 +74,6 @@ let lastContextDependency: | ContextDependency | ContextDependencyWithSelect | null = null; -let lastFullyObservedContext: ReactContext | null = null; let isDisallowedContextReadInDEV: boolean = false; @@ -83,7 +82,6 @@ export function resetContextDependencies(): void { // cannot be called outside the render phase. currentlyRenderingFiber = null; lastContextDependency = null; - lastFullyObservedContext = null; if (__DEV__) { isDisallowedContextReadInDEV = false; } @@ -730,7 +728,6 @@ export function prepareToReadContext( ): void { currentlyRenderingFiber = workInProgress; lastContextDependency = null; - lastFullyObservedContext = null; const dependencies = workInProgress.dependencies; if (dependencies !== null) { @@ -802,46 +799,42 @@ function readContextForConsumer_withSelect( ? context._currentValue : context._currentValue2; - if (lastFullyObservedContext === context) { - // Nothing to do. We already observe everything in this context. - } else { - const contextItem = { - context: ((context: any): ReactContext), - memoizedValue: value, - next: null, - select: ((select: any): (context: mixed) => Array), - lastSelectedValue: select(value), - }; - - if (lastContextDependency === null) { - if (consumer === null) { - throw new Error( - 'Context can only be read while React is rendering. ' + - 'In classes, you can read it in the render method or getDerivedStateFromProps. ' + - 'In function components, you can read it directly in the function body, but not ' + - 'inside Hooks like useReducer() or useMemo().', - ); - } + const contextItem = { + context: ((context: any): ReactContext), + memoizedValue: value, + next: null, + select: ((select: any): (context: mixed) => Array), + lastSelectedValue: select(value), + }; + + if (lastContextDependency === null) { + if (consumer === null) { + throw new Error( + 'Context can only be read while React is rendering. ' + + 'In classes, you can read it in the render method or getDerivedStateFromProps. ' + + 'In function components, you can read it directly in the function body, but not ' + + 'inside Hooks like useReducer() or useMemo().', + ); + } - // This is the first dependency for this component. Create a new list. - lastContextDependency = contextItem; - consumer.dependencies = __DEV__ - ? { - lanes: NoLanes, - firstContext: contextItem, - _debugThenableState: null, - } - : { - lanes: NoLanes, - firstContext: contextItem, - }; - if (enableLazyContextPropagation) { - consumer.flags |= NeedsPropagation; - } - } else { - // Append a new context item. - lastContextDependency = lastContextDependency.next = contextItem; + // This is the first dependency for this component. Create a new list. + lastContextDependency = contextItem; + consumer.dependencies = __DEV__ + ? { + lanes: NoLanes, + firstContext: contextItem, + _debugThenableState: null, + } + : { + lanes: NoLanes, + firstContext: contextItem, + }; + if (enableLazyContextPropagation) { + consumer.flags |= NeedsPropagation; } + } else { + // Append a new context item. + lastContextDependency = lastContextDependency.next = contextItem; } return value; } @@ -854,44 +847,40 @@ function readContextForConsumer( ? context._currentValue : context._currentValue2; - if (lastFullyObservedContext === context) { - // Nothing to do. We already observe everything in this context. - } else { - const contextItem = { - context: ((context: any): ReactContext), - memoizedValue: value, - next: null, - }; - - if (lastContextDependency === null) { - if (consumer === null) { - throw new Error( - 'Context can only be read while React is rendering. ' + - 'In classes, you can read it in the render method or getDerivedStateFromProps. ' + - 'In function components, you can read it directly in the function body, but not ' + - 'inside Hooks like useReducer() or useMemo().', - ); - } + const contextItem = { + context: ((context: any): ReactContext), + memoizedValue: value, + next: null, + }; - // This is the first dependency for this component. Create a new list. - lastContextDependency = contextItem; - consumer.dependencies = __DEV__ - ? { - lanes: NoLanes, - firstContext: contextItem, - _debugThenableState: null, - } - : { - lanes: NoLanes, - firstContext: contextItem, - }; - if (enableLazyContextPropagation) { - consumer.flags |= NeedsPropagation; - } - } else { - // Append a new context item. - lastContextDependency = lastContextDependency.next = contextItem; + if (lastContextDependency === null) { + if (consumer === null) { + throw new Error( + 'Context can only be read while React is rendering. ' + + 'In classes, you can read it in the render method or getDerivedStateFromProps. ' + + 'In function components, you can read it directly in the function body, but not ' + + 'inside Hooks like useReducer() or useMemo().', + ); + } + + // This is the first dependency for this component. Create a new list. + lastContextDependency = contextItem; + consumer.dependencies = __DEV__ + ? { + lanes: NoLanes, + firstContext: contextItem, + _debugThenableState: null, + } + : { + lanes: NoLanes, + firstContext: contextItem, + }; + if (enableLazyContextPropagation) { + consumer.flags |= NeedsPropagation; } + } else { + // Append a new context item. + lastContextDependency = lastContextDependency.next = contextItem; } return value; }