GeezerVison is showing github.com — Add StrictMode level prop and createRoot unstable_strictModeLevel opt… · react/react@9209c30
File treeExpand file treeCollapse file tree Open diff view settings react-native-renderer/src
Some content is hiddenLarge Commits have some content hidden by default. Use the searchbox below for content that may be hidden. Dismiss bannerExpand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: packages/react-dom/src/client/ReactDOMRoot.js +13-1Lines changed: 13 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -27,6 +27,7 @@ export type RootOptions = { | 27 | 27 | mutableSources?: Array<MutableSource<any>>, | 28 | 28 | ... | 29 | 29 | }, | | 30 | + unstable_strictModeLevel?: number, | 30 | 31 | ... | 31 | 32 | }; | 32 | 33 |
| @@ -128,7 +129,18 @@ function createRootImpl( | 128 | 129 | options.hydrationOptions != null && | 129 | 130 | options.hydrationOptions.mutableSources) || | 130 | 131 | null; | 131 | | - const root = createContainer(container, tag, hydrate, hydrationCallbacks); | | 132 | + const strictModeLevelOverride = | | 133 | + options != null && options.unstable_strictModeLevel != null | | 134 | + ? options.unstable_strictModeLevel | | 135 | + : null; | | 136 | + | | 137 | + const root = createContainer( | | 138 | + container, | | 139 | + tag, | | 140 | + hydrate, | | 141 | + hydrationCallbacks, | | 142 | + strictModeLevelOverride, | | 143 | + ); | 132 | 144 | markContainerAsRoot(root.current, container); | 133 | 145 |
| 134 | 146 | const rootContainerElement = | |
Collapse file Copy file name to clipboardExpand all lines: packages/react-native-renderer/src/ReactFabric.js +1-1Lines changed: 1 addition & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -203,7 +203,7 @@ function render( | 203 | 203 | if (!root) { | 204 | 204 | // TODO (bvaughn): If we decide to keep the wrapper component, | 205 | 205 | // We could create a wrapper for containerTag as well to reduce special casing. | 206 | | - root = createContainer(containerTag, LegacyRoot, false, null); | | 206 | + root = createContainer(containerTag, LegacyRoot, false, null, null); | 207 | 207 | roots.set(containerTag, root); | 208 | 208 | } | 209 | 209 | updateContainer(element, root, null, callback); | |
Collapse file Copy file name to clipboardExpand all lines: packages/react-native-renderer/src/ReactNativeRenderer.js +1-1Lines changed: 1 addition & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -202,7 +202,7 @@ function render( | 202 | 202 | if (!root) { | 203 | 203 | // TODO (bvaughn): If we decide to keep the wrapper component, | 204 | 204 | // We could create a wrapper for containerTag as well to reduce special casing. | 205 | | - root = createContainer(containerTag, LegacyRoot, false, null); | | 205 | + root = createContainer(containerTag, LegacyRoot, false, null, null); | 206 | 206 | roots.set(containerTag, root); | 207 | 207 | } | 208 | 208 | updateContainer(element, root, null, callback); | |
Collapse file Copy file name to clipboardExpand all lines: packages/react-noop-renderer/src/createReactNoop.js +4-1Lines changed: 4 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -722,7 +722,7 @@ function createReactNoop(reconciler: Function, useMutation: boolean) { | 722 | 722 | if (!root) { | 723 | 723 | const container = {rootID: rootID, pendingChildren: [], children: []}; | 724 | 724 | rootContainers.set(rootID, container); | 725 | | - root = NoopRenderer.createContainer(container, tag, false, null); | | 725 | + root = NoopRenderer.createContainer(container, tag, false, null, null); | 726 | 726 | roots.set(rootID, root); | 727 | 727 | } | 728 | 728 | return root.current.stateNode.containerInfo; | @@ -740,6 +740,7 @@ function createReactNoop(reconciler: Function, useMutation: boolean) { | 740 | 740 | ConcurrentRoot, | 741 | 741 | false, | 742 | 742 | null, | | 743 | + null, | 743 | 744 | ); | 744 | 745 | return { | 745 | 746 | _Scheduler: Scheduler, | @@ -766,6 +767,7 @@ function createReactNoop(reconciler: Function, useMutation: boolean) { | 766 | 767 | BlockingRoot, | 767 | 768 | false, | 768 | 769 | null, | | 770 | + null, | 769 | 771 | ); | 770 | 772 | return { | 771 | 773 | _Scheduler: Scheduler, | @@ -792,6 +794,7 @@ function createReactNoop(reconciler: Function, useMutation: boolean) { | 792 | 794 | LegacyRoot, | 793 | 795 | false, | 794 | 796 | null, | | 797 | + null, | 795 | 798 | ); | 796 | 799 | return { | 797 | 800 | _Scheduler: Scheduler, | |
Collapse file Copy file name to clipboardExpand all lines: packages/react-reconciler/src/ReactChildFiber.new.js +2-2Lines changed: 2 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -46,7 +46,7 @@ import { | 46 | 46 | } from './ReactFiber.new'; | 47 | 47 | import {emptyRefsObject} from './ReactFiberClassComponent.new'; | 48 | 48 | import {isCompatibleFamilyForHotReloading} from './ReactFiberHotReloading.new'; | 49 | | -import {StrictMode} from './ReactTypeOfMode'; | | 49 | +import {StrictLegacyMode} from './ReactTypeOfMode'; | 50 | 50 |
| 51 | 51 | let didWarnAboutMaps; | 52 | 52 | let didWarnAboutGenerators; | @@ -114,7 +114,7 @@ function coerceRef( | 114 | 114 | // TODO: Clean this up once we turn on the string ref warning for | 115 | 115 | // everyone, because the strict mode case will no longer be relevant | 116 | 116 | if ( | 117 | | - (returnFiber.mode & StrictMode || warnAboutStringRefs) && | | 117 | + (returnFiber.mode & StrictLegacyMode || warnAboutStringRefs) && | 118 | 118 | // We warn in ReactElement.js if owner and self are equal for string refs | 119 | 119 | // because these cannot be automatically converted to an arrow function | 120 | 120 | // using a codemod. Therefore, we don't have to warn about string refs again. | |
Collapse file Copy file name to clipboardExpand all lines: packages/react-reconciler/src/ReactChildFiber.old.js +2-2Lines changed: 2 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -46,7 +46,7 @@ import { | 46 | 46 | } from './ReactFiber.old'; | 47 | 47 | import {emptyRefsObject} from './ReactFiberClassComponent.old'; | 48 | 48 | import {isCompatibleFamilyForHotReloading} from './ReactFiberHotReloading.old'; | 49 | | -import {StrictMode} from './ReactTypeOfMode'; | | 49 | +import {StrictLegacyMode} from './ReactTypeOfMode'; | 50 | 50 |
| 51 | 51 | let didWarnAboutMaps; | 52 | 52 | let didWarnAboutGenerators; | @@ -114,7 +114,7 @@ function coerceRef( | 114 | 114 | // TODO: Clean this up once we turn on the string ref warning for | 115 | 115 | // everyone, because the strict mode case will no longer be relevant | 116 | 116 | if ( | 117 | | - (returnFiber.mode & StrictMode || warnAboutStringRefs) && | | 117 | + (returnFiber.mode & StrictLegacyMode || warnAboutStringRefs) && | 118 | 118 | // We warn in ReactElement.js if owner and self are equal for string refs | 119 | 119 | // because these cannot be automatically converted to an arrow function | 120 | 120 | // using a codemod. Therefore, we don't have to warn about string refs again. | |
Collapse file Copy file name to clipboardExpand all lines: packages/react-reconciler/src/ReactFiber.new.js +58-6Lines changed: 58 additions & 6 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -19,9 +19,11 @@ import type {OffscreenProps} from './ReactFiberOffscreenComponent'; | 19 | 19 |
| 20 | 20 | import invariant from 'shared/invariant'; | 21 | 21 | import { | | 22 | + createRootStrictEffectsByDefault, | | 23 | + enableCache, | | 24 | + enableStrictEffects, | 22 | 25 | enableProfilerTimer, | 23 | 26 | enableScopeAPI, | 24 | | - enableCache, | 25 | 27 | } from 'shared/ReactFeatureFlags'; | 26 | 28 | import {NoFlags, Placement, StaticMask} from './ReactFiberFlags'; | 27 | 29 | import {ConcurrentRoot, BlockingRoot} from './ReactRootTags'; | @@ -64,7 +66,8 @@ import { | 64 | 66 | ConcurrentMode, | 65 | 67 | DebugTracingMode, | 66 | 68 | ProfileMode, | 67 | | - StrictMode, | | 69 | + StrictLegacyMode, | | 70 | + StrictEffectsMode, | 68 | 71 | BlockingMode, | 69 | 72 | } from './ReactTypeOfMode'; | 70 | 73 | import { | @@ -418,12 +421,47 @@ export function resetWorkInProgress(workInProgress: Fiber, renderLanes: Lanes) { | 418 | 421 | return workInProgress; | 419 | 422 | } | 420 | 423 |
| 421 | | -export function createHostRootFiber(tag: RootTag): Fiber { | | 424 | +export function createHostRootFiber( | | 425 | + tag: RootTag, | | 426 | + strictModeLevelOverride: null | number, | | 427 | +): Fiber { | 422 | 428 | let mode; | 423 | 429 | if (tag === ConcurrentRoot) { | 424 | | - mode = ConcurrentMode | BlockingMode | StrictMode; | | 430 | + mode = ConcurrentMode | BlockingMode; | | 431 | + if (strictModeLevelOverride !== null) { | | 432 | + if (strictModeLevelOverride >= 1) { | | 433 | + mode |= StrictLegacyMode; | | 434 | + } | | 435 | + if (enableStrictEffects) { | | 436 | + if (strictModeLevelOverride >= 2) { | | 437 | + mode |= StrictEffectsMode; | | 438 | + } | | 439 | + } | | 440 | + } else { | | 441 | + if (enableStrictEffects && createRootStrictEffectsByDefault) { | | 442 | + mode |= StrictLegacyMode | StrictEffectsMode; | | 443 | + } else { | | 444 | + mode |= StrictLegacyMode; | | 445 | + } | | 446 | + } | 425 | 447 | } else if (tag === BlockingRoot) { | 426 | | - mode = BlockingMode | StrictMode; | | 448 | + mode = BlockingMode; | | 449 | + if (strictModeLevelOverride !== null) { | | 450 | + if (strictModeLevelOverride >= 1) { | | 451 | + mode |= StrictLegacyMode; | | 452 | + } | | 453 | + if (enableStrictEffects) { | | 454 | + if (strictModeLevelOverride >= 2) { | | 455 | + mode |= StrictEffectsMode; | | 456 | + } | | 457 | + } | | 458 | + } else { | | 459 | + if (enableStrictEffects && createRootStrictEffectsByDefault) { | | 460 | + mode |= StrictLegacyMode | StrictEffectsMode; | | 461 | + } else { | | 462 | + mode |= StrictLegacyMode; | | 463 | + } | | 464 | + } | 427 | 465 | } else { | 428 | 466 | mode = NoMode; | 429 | 467 | } | @@ -472,7 +510,21 @@ export function createFiberFromTypeAndProps( | 472 | 510 | break; | 473 | 511 | case REACT_STRICT_MODE_TYPE: | 474 | 512 | fiberTag = Mode; | 475 | | - mode |= StrictMode; | | 513 | + | | 514 | + // Legacy strict mode (<StrictMode> without any level prop) defaults to level 1. | | 515 | + const level = | | 516 | + pendingProps.unstable_level == null ? 1 : pendingProps.unstable_level; | | 517 | + | | 518 | + // Levels cascade; higher levels inherit all lower level modes. | | 519 | + // It is explicitly not supported to lower a mode with nesting, only to increase it. | | 520 | + if (level >= 1) { | | 521 | + mode |= StrictLegacyMode; | | 522 | + } | | 523 | + if (enableStrictEffects) { | | 524 | + if (level >= 2) { | | 525 | + mode |= StrictEffectsMode; | | 526 | + } | | 527 | + } | 476 | 528 | break; | 477 | 529 | case REACT_PROFILER_TYPE: | 478 | 530 | return createFiberFromProfiler(pendingProps, mode, lanes, key); | |
Collapse file Copy file name to clipboardExpand all lines: packages/react-reconciler/src/ReactFiber.old.js +58-6Lines changed: 58 additions & 6 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -19,9 +19,11 @@ import type {OffscreenProps} from './ReactFiberOffscreenComponent'; | 19 | 19 |
| 20 | 20 | import invariant from 'shared/invariant'; | 21 | 21 | import { | | 22 | + createRootStrictEffectsByDefault, | | 23 | + enableCache, | | 24 | + enableStrictEffects, | 22 | 25 | enableProfilerTimer, | 23 | 26 | enableScopeAPI, | 24 | | - enableCache, | 25 | 27 | } from 'shared/ReactFeatureFlags'; | 26 | 28 | import {NoFlags, Placement, StaticMask} from './ReactFiberFlags'; | 27 | 29 | import {ConcurrentRoot, BlockingRoot} from './ReactRootTags'; | @@ -64,7 +66,8 @@ import { | 64 | 66 | ConcurrentMode, | 65 | 67 | DebugTracingMode, | 66 | 68 | ProfileMode, | 67 | | - StrictMode, | | 69 | + StrictLegacyMode, | | 70 | + StrictEffectsMode, | 68 | 71 | BlockingMode, | 69 | 72 | } from './ReactTypeOfMode'; | 70 | 73 | import { | @@ -418,12 +421,47 @@ export function resetWorkInProgress(workInProgress: Fiber, renderLanes: Lanes) { | 418 | 421 | return workInProgress; | 419 | 422 | } | 420 | 423 |
| 421 | | -export function createHostRootFiber(tag: RootTag): Fiber { | | 424 | +export function createHostRootFiber( | | 425 | + tag: RootTag, | | 426 | + strictModeLevelOverride: null | number, | | 427 | +): Fiber { | 422 | 428 | let mode; | 423 | 429 | if (tag === ConcurrentRoot) { | 424 | | - mode = ConcurrentMode | BlockingMode | StrictMode; | | 430 | + mode = ConcurrentMode | BlockingMode; | | 431 | + if (strictModeLevelOverride !== null) { | | 432 | + if (strictModeLevelOverride >= 1) { | | 433 | + mode |= StrictLegacyMode; | | 434 | + } | | 435 | + if (enableStrictEffects) { | | 436 | + if (strictModeLevelOverride >= 2) { | | 437 | + mode |= StrictEffectsMode; | | 438 | + } | | 439 | + } | | 440 | + } else { | | 441 | + if (enableStrictEffects && createRootStrictEffectsByDefault) { | | 442 | + mode |= StrictLegacyMode | StrictEffectsMode; | | 443 | + } else { | | 444 | + mode |= StrictLegacyMode; | | 445 | + } | | 446 | + } | 425 | 447 | } else if (tag === BlockingRoot) { | 426 | | - mode = BlockingMode | StrictMode; | | 448 | + mode = BlockingMode; | | 449 | + if (strictModeLevelOverride !== null) { | | 450 | + if (strictModeLevelOverride >= 1) { | | 451 | + mode |= StrictLegacyMode; | | 452 | + } | | 453 | + if (enableStrictEffects) { | | 454 | + if (strictModeLevelOverride >= 2) { | | 455 | + mode |= StrictEffectsMode; | | 456 | + } | | 457 | + } | | 458 | + } else { | | 459 | + if (enableStrictEffects && createRootStrictEffectsByDefault) { | | 460 | + mode |= StrictLegacyMode | StrictEffectsMode; | | 461 | + } else { | | 462 | + mode |= StrictLegacyMode; | | 463 | + } | | 464 | + } | 427 | 465 | } else { | 428 | 466 | mode = NoMode; | 429 | 467 | } | @@ -472,7 +510,21 @@ export function createFiberFromTypeAndProps( | 472 | 510 | break; | 473 | 511 | case REACT_STRICT_MODE_TYPE: | 474 | 512 | fiberTag = Mode; | 475 | | - mode |= StrictMode; | | 513 | + | | 514 | + // Legacy strict mode (<StrictMode> without any level prop) defaults to level 1. | | 515 | + const level = | | 516 | + pendingProps.unstable_level == null ? 1 : pendingProps.unstable_level; | | 517 | + | | 518 | + // Levels cascade; higher levels inherit all lower level modes. | | 519 | + // It is explicitly not supported to lower a mode with nesting, only to increase it. | | 520 | + if (level >= 1) { | | 521 | + mode |= StrictLegacyMode; | | 522 | + } | | 523 | + if (enableStrictEffects) { | | 524 | + if (level >= 2) { | | 525 | + mode |= StrictEffectsMode; | | 526 | + } | | 527 | + } | 476 | 528 | break; | 477 | 529 | case REACT_PROFILER_TYPE: | 478 | 530 | return createFiberFromProfiler(pendingProps, mode, lanes, key); | |
Collapse file Copy file name to clipboardExpand all lines: packages/react-reconciler/src/ReactFiberBeginWork.new.js +6-6Lines changed: 6 additions & 6 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -125,7 +125,7 @@ import { | 125 | 125 | ConcurrentMode, | 126 | 126 | NoMode, | 127 | 127 | ProfileMode, | 128 | | - StrictMode, | | 128 | + StrictLegacyMode, | 129 | 129 | BlockingMode, | 130 | 130 | } from './ReactTypeOfMode'; | 131 | 131 | import { | @@ -357,7 +357,7 @@ function updateForwardRef( | 357 | 357 | ); | 358 | 358 | if ( | 359 | 359 | debugRenderPhaseSideEffectsForStrictMode && | 360 | | - workInProgress.mode & StrictMode | | 360 | + workInProgress.mode & StrictLegacyMode | 361 | 361 | ) { | 362 | 362 | disableLogs(); | 363 | 363 | try { | @@ -889,7 +889,7 @@ function updateFunctionComponent( | 889 | 889 | ); | 890 | 890 | if ( | 891 | 891 | debugRenderPhaseSideEffectsForStrictMode && | 892 | | - workInProgress.mode & StrictMode | | 892 | + workInProgress.mode & StrictLegacyMode | 893 | 893 | ) { | 894 | 894 | disableLogs(); | 895 | 895 | try { | @@ -1068,7 +1068,7 @@ function finishClassComponent( | 1068 | 1068 | nextChildren = instance.render(); | 1069 | 1069 | if ( | 1070 | 1070 | debugRenderPhaseSideEffectsForStrictMode && | 1071 | | - workInProgress.mode & StrictMode | | 1071 | + workInProgress.mode & StrictLegacyMode | 1072 | 1072 | ) { | 1073 | 1073 | disableLogs(); | 1074 | 1074 | try { | @@ -1478,7 +1478,7 @@ function mountIndeterminateComponent( | 1478 | 1478 | } | 1479 | 1479 | } | 1480 | 1480 |
| 1481 | | - if (workInProgress.mode & StrictMode) { | | 1481 | + if (workInProgress.mode & StrictLegacyMode) { | 1482 | 1482 | ReactStrictModeWarnings.recordLegacyContextWarning(workInProgress, null); | 1483 | 1483 | } | 1484 | 1484 |
| @@ -1615,7 +1615,7 @@ function mountIndeterminateComponent( | 1615 | 1615 |
| 1616 | 1616 | if ( | 1617 | 1617 | debugRenderPhaseSideEffectsForStrictMode && | 1618 | | - workInProgress.mode & StrictMode | | 1618 | + workInProgress.mode & StrictLegacyMode | 1619 | 1619 | ) { | 1620 | 1620 | disableLogs(); | 1621 | 1621 | try { | |
|