GeezerVison is showing github.com — Move context comparison to consumer · react/react@258b375
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: packages/react-dom/src/__tests__/ReactLegacyContextDisabled-test.internal.js +9-1Lines changed: 9 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -226,7 +226,15 @@ describe('ReactLegacyContextDisabled', () => { | 226 | 226 | container, | 227 | 227 | ); | 228 | 228 | expect(container.textContent).toBe('bbb'); | 229 | | - expect(lifecycleContextLog).toEqual(['b', 'b']); // sCU skipped due to changed context value. | | 229 | + if (gate(flags => flags.enableLazyContextPropagation)) { | | 230 | + // In the lazy propagation implementation, we don't check if context | | 231 | + // changed until after shouldComponentUpdate is run. | | 232 | + expect(lifecycleContextLog).toEqual(['b', 'b', 'b']); | | 233 | + } else { | | 234 | + // In the eager implementation, a dirty flag was set when the parent | | 235 | + // changed, so we skipped sCU. | | 236 | + expect(lifecycleContextLog).toEqual(['b', 'b']); | | 237 | + } | 230 | 238 | ReactDOM.unmountComponentAtNode(container); | 231 | 239 | }); | 232 | 240 | }); |
Collapse file Copy file name to clipboardExpand all lines: packages/react-reconciler/src/ReactFiberBeginWork.new.js +4Lines changed: 4 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -3074,6 +3074,10 @@ export function markWorkInProgressReceivedUpdate() { | 3074 | 3074 | didReceiveUpdate = true; | 3075 | 3075 | } | 3076 | 3076 |
| | 3077 | +export function checkIfWorkInProgressReceivedUpdate() { | | 3078 | + return didReceiveUpdate; | | 3079 | +} | | 3080 | + | 3077 | 3081 | function bailoutOnAlreadyFinishedWork( | 3078 | 3082 | current: Fiber | null, | 3079 | 3083 | workInProgress: Fiber, | |
Collapse file Copy file name to clipboardExpand all lines: packages/react-reconciler/src/ReactFiberBeginWork.old.js +4Lines changed: 4 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -3074,6 +3074,10 @@ export function markWorkInProgressReceivedUpdate() { | 3074 | 3074 | didReceiveUpdate = true; | 3075 | 3075 | } | 3076 | 3076 |
| | 3077 | +export function checkIfWorkInProgressReceivedUpdate() { | | 3078 | + return didReceiveUpdate; | | 3079 | +} | | 3080 | + | 3077 | 3081 | function bailoutOnAlreadyFinishedWork( | 3078 | 3082 | current: Fiber | null, | 3079 | 3083 | workInProgress: Fiber, | |
Collapse file Copy file name to clipboardExpand all lines: packages/react-reconciler/src/ReactFiberClassComponent.new.js +18-3Lines changed: 18 additions & 3 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -20,6 +20,7 @@ import { | 20 | 20 | enableSchedulingProfiler, | 21 | 21 | warnAboutDeprecatedLifecycles, | 22 | 22 | enableStrictEffects, | | 23 | + enableLazyContextPropagation, | 23 | 24 | } from 'shared/ReactFeatureFlags'; | 24 | 25 | import ReactStrictModeWarnings from './ReactStrictModeWarnings.new'; | 25 | 26 | import {isMounted} from './ReactFiberTreeReflection'; | @@ -58,7 +59,7 @@ import { | 58 | 59 | hasContextChanged, | 59 | 60 | emptyContextObject, | 60 | 61 | } from './ReactFiberContext.new'; | 61 | | -import {readContext} from './ReactFiberNewContext.new'; | | 62 | +import {readContext, checkIfContextChanged} from './ReactFiberNewContext.new'; | 62 | 63 | import { | 63 | 64 | requestEventTime, | 64 | 65 | requestUpdateLane, | @@ -1150,7 +1151,13 @@ function updateClassInstance( | 1150 | 1151 | unresolvedOldProps === unresolvedNewProps && | 1151 | 1152 | oldState === newState && | 1152 | 1153 | !hasContextChanged() && | 1153 | | - !checkHasForceUpdateAfterProcessing() | | 1154 | + !checkHasForceUpdateAfterProcessing() && | | 1155 | + !( | | 1156 | + enableLazyContextPropagation && | | 1157 | + current !== null && | | 1158 | + current.dependencies !== null && | | 1159 | + checkIfContextChanged(current.dependencies) | | 1160 | + ) | 1154 | 1161 | ) { | 1155 | 1162 | // If an update was already in progress, we should schedule an Update | 1156 | 1163 | // effect even though we're bailing out, so that cWU/cDU are called. | @@ -1193,7 +1200,15 @@ function updateClassInstance( | 1193 | 1200 | oldState, | 1194 | 1201 | newState, | 1195 | 1202 | nextContext, | 1196 | | - ); | | 1203 | + ) || | | 1204 | + // TODO: In some cases, we'll end up checking if context has changed twice, | | 1205 | + // both before and after `shouldComponentUpdate` has been called. Not ideal, | | 1206 | + // but I'm loath to refactor this function. This only happens for memoized | | 1207 | + // components so it's not that common. | | 1208 | + (enableLazyContextPropagation && | | 1209 | + current !== null && | | 1210 | + current.dependencies !== null && | | 1211 | + checkIfContextChanged(current.dependencies)); | 1197 | 1212 |
| 1198 | 1213 | if (shouldUpdate) { | 1199 | 1214 | // In order to support react-lifecycles-compat polyfilled components, | |
Collapse file Copy file name to clipboardExpand all lines: packages/react-reconciler/src/ReactFiberClassComponent.old.js +18-3Lines changed: 18 additions & 3 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -20,6 +20,7 @@ import { | 20 | 20 | enableSchedulingProfiler, | 21 | 21 | warnAboutDeprecatedLifecycles, | 22 | 22 | enableStrictEffects, | | 23 | + enableLazyContextPropagation, | 23 | 24 | } from 'shared/ReactFeatureFlags'; | 24 | 25 | import ReactStrictModeWarnings from './ReactStrictModeWarnings.old'; | 25 | 26 | import {isMounted} from './ReactFiberTreeReflection'; | @@ -58,7 +59,7 @@ import { | 58 | 59 | hasContextChanged, | 59 | 60 | emptyContextObject, | 60 | 61 | } from './ReactFiberContext.old'; | 61 | | -import {readContext} from './ReactFiberNewContext.old'; | | 62 | +import {readContext, checkIfContextChanged} from './ReactFiberNewContext.old'; | 62 | 63 | import { | 63 | 64 | requestEventTime, | 64 | 65 | requestUpdateLane, | @@ -1150,7 +1151,13 @@ function updateClassInstance( | 1150 | 1151 | unresolvedOldProps === unresolvedNewProps && | 1151 | 1152 | oldState === newState && | 1152 | 1153 | !hasContextChanged() && | 1153 | | - !checkHasForceUpdateAfterProcessing() | | 1154 | + !checkHasForceUpdateAfterProcessing() && | | 1155 | + !( | | 1156 | + enableLazyContextPropagation && | | 1157 | + current !== null && | | 1158 | + current.dependencies !== null && | | 1159 | + checkIfContextChanged(current.dependencies) | | 1160 | + ) | 1154 | 1161 | ) { | 1155 | 1162 | // If an update was already in progress, we should schedule an Update | 1156 | 1163 | // effect even though we're bailing out, so that cWU/cDU are called. | @@ -1193,7 +1200,15 @@ function updateClassInstance( | 1193 | 1200 | oldState, | 1194 | 1201 | newState, | 1195 | 1202 | nextContext, | 1196 | | - ); | | 1203 | + ) || | | 1204 | + // TODO: In some cases, we'll end up checking if context has changed twice, | | 1205 | + // both before and after `shouldComponentUpdate` has been called. Not ideal, | | 1206 | + // but I'm loath to refactor this function. This only happens for memoized | | 1207 | + // components so it's not that common. | | 1208 | + (enableLazyContextPropagation && | | 1209 | + current !== null && | | 1210 | + current.dependencies !== null && | | 1211 | + checkIfContextChanged(current.dependencies)); | 1197 | 1212 |
| 1198 | 1213 | if (shouldUpdate) { | 1199 | 1214 | // In order to support react-lifecycles-compat polyfilled components, | |
Collapse file Copy file name to clipboardExpand all lines: packages/react-reconciler/src/ReactFiberHooks.new.js +27-2Lines changed: 27 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -30,6 +30,7 @@ import { | 30 | 30 | decoupleUpdatePriorityFromScheduler, | 31 | 31 | enableUseRefAccessWarning, | 32 | 32 | enableStrictEffects, | | 33 | + enableLazyContextPropagation, | 33 | 34 | } from 'shared/ReactFeatureFlags'; | 34 | 35 |
| 35 | 36 | import { | @@ -54,7 +55,7 @@ import { | 54 | 55 | higherLanePriority, | 55 | 56 | DefaultLanePriority, | 56 | 57 | } from './ReactFiberLane.new'; | 57 | | -import {readContext} from './ReactFiberNewContext.new'; | | 58 | +import {readContext, checkIfContextChanged} from './ReactFiberNewContext.new'; | 58 | 59 | import {HostRoot, CacheComponent} from './ReactWorkTags'; | 59 | 60 | import { | 60 | 61 | Update as UpdateEffect, | @@ -83,7 +84,10 @@ import { | 83 | 84 | import invariant from 'shared/invariant'; | 84 | 85 | import getComponentNameFromFiber from 'react-reconciler/src/getComponentNameFromFiber'; | 85 | 86 | import is from 'shared/objectIs'; | 86 | | -import {markWorkInProgressReceivedUpdate} from './ReactFiberBeginWork.new'; | | 87 | +import { | | 88 | + markWorkInProgressReceivedUpdate, | | 89 | + checkIfWorkInProgressReceivedUpdate, | | 90 | +} from './ReactFiberBeginWork.new'; | 87 | 91 | import { | 88 | 92 | UserBlockingPriority, | 89 | 93 | NormalPriority, | @@ -496,6 +500,27 @@ export function renderWithHooks<Props, SecondArg>( | 496 | 500 | 'early return statement.', | 497 | 501 | ); | 498 | 502 |
| | 503 | + if (enableLazyContextPropagation) { | | 504 | + if (current !== null) { | | 505 | + if (!checkIfWorkInProgressReceivedUpdate()) { | | 506 | + // If there were no changes to props or state, we need to check if there | | 507 | + // was a context change. We didn't already do this because there's no | | 508 | + // 1:1 correspondence between dependencies and hooks. Although, because | | 509 | + // there almost always is in the common case (`readContext` is an | | 510 | + // internal API), we could compare in there. OTOH, we only hit this case | | 511 | + // if everything else bails out, so on the whole it might be better to | | 512 | + // keep the comparison out of the common path. | | 513 | + const currentDependencies = current.dependencies; | | 514 | + if ( | | 515 | + currentDependencies !== null && | | 516 | + checkIfContextChanged(currentDependencies) | | 517 | + ) { | | 518 | + markWorkInProgressReceivedUpdate(); | | 519 | + } | | 520 | + } | | 521 | + } | | 522 | + } | | 523 | + | 499 | 524 | return children; | 500 | 525 | } | 501 | 526 |
| |
Collapse file Copy file name to clipboardExpand all lines: packages/react-reconciler/src/ReactFiberHooks.old.js +27-2Lines changed: 27 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -30,6 +30,7 @@ import { | 30 | 30 | decoupleUpdatePriorityFromScheduler, | 31 | 31 | enableUseRefAccessWarning, | 32 | 32 | enableStrictEffects, | | 33 | + enableLazyContextPropagation, | 33 | 34 | } from 'shared/ReactFeatureFlags'; | 34 | 35 |
| 35 | 36 | import { | @@ -54,7 +55,7 @@ import { | 54 | 55 | higherLanePriority, | 55 | 56 | DefaultLanePriority, | 56 | 57 | } from './ReactFiberLane.old'; | 57 | | -import {readContext} from './ReactFiberNewContext.old'; | | 58 | +import {readContext, checkIfContextChanged} from './ReactFiberNewContext.old'; | 58 | 59 | import {HostRoot, CacheComponent} from './ReactWorkTags'; | 59 | 60 | import { | 60 | 61 | Update as UpdateEffect, | @@ -83,7 +84,10 @@ import { | 83 | 84 | import invariant from 'shared/invariant'; | 84 | 85 | import getComponentNameFromFiber from 'react-reconciler/src/getComponentNameFromFiber'; | 85 | 86 | import is from 'shared/objectIs'; | 86 | | -import {markWorkInProgressReceivedUpdate} from './ReactFiberBeginWork.old'; | | 87 | +import { | | 88 | + markWorkInProgressReceivedUpdate, | | 89 | + checkIfWorkInProgressReceivedUpdate, | | 90 | +} from './ReactFiberBeginWork.old'; | 87 | 91 | import { | 88 | 92 | UserBlockingPriority, | 89 | 93 | NormalPriority, | @@ -496,6 +500,27 @@ export function renderWithHooks<Props, SecondArg>( | 496 | 500 | 'early return statement.', | 497 | 501 | ); | 498 | 502 |
| | 503 | + if (enableLazyContextPropagation) { | | 504 | + if (current !== null) { | | 505 | + if (!checkIfWorkInProgressReceivedUpdate()) { | | 506 | + // If there were no changes to props or state, we need to check if there | | 507 | + // was a context change. We didn't already do this because there's no | | 508 | + // 1:1 correspondence between dependencies and hooks. Although, because | | 509 | + // there almost always is in the common case (`readContext` is an | | 510 | + // internal API), we could compare in there. OTOH, we only hit this case | | 511 | + // if everything else bails out, so on the whole it might be better to | | 512 | + // keep the comparison out of the common path. | | 513 | + const currentDependencies = current.dependencies; | | 514 | + if ( | | 515 | + currentDependencies !== null && | | 516 | + checkIfContextChanged(currentDependencies) | | 517 | + ) { | | 518 | + markWorkInProgressReceivedUpdate(); | | 519 | + } | | 520 | + } | | 521 | + } | | 522 | + } | | 523 | + | 499 | 524 | return children; | 500 | 525 | } | 501 | 526 |
| |
|