GeezerVison is showing github.com — [Offscreen] Mount/unmount layout effects (#21386) · react/react@e16d61c
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-reconciler/src/ReactFiberCommitWork.new.js +27-20Lines changed: 27 additions & 20 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -2306,33 +2306,40 @@ function commitLayoutEffects_begin( | 2306 | 2306 | const fiber = nextEffect; | 2307 | 2307 | const firstChild = fiber.child; | 2308 | 2308 |
| 2309 | | - if (enableSuspenseLayoutEffectSemantics && isModernRoot) { | | 2309 | + if ( | | 2310 | + enableSuspenseLayoutEffectSemantics && | | 2311 | + fiber.tag === OffscreenComponent && | | 2312 | + isModernRoot | | 2313 | + ) { | 2310 | 2314 | // Keep track of the current Offscreen stack's state. | 2311 | | - if (fiber.tag === OffscreenComponent) { | 2312 | | - const current = fiber.alternate; | 2313 | | - const wasHidden = current !== null && current.memoizedState !== null; | 2314 | | - const isHidden = fiber.memoizedState !== null; | 2315 | | - | 2316 | | - const newOffscreenSubtreeIsHidden = | 2317 | | - isHidden || offscreenSubtreeIsHidden; | 2318 | | - const newOffscreenSubtreeWasHidden = | 2319 | | - wasHidden || offscreenSubtreeWasHidden; | 2320 | | - | 2321 | | - if ( | 2322 | | - newOffscreenSubtreeIsHidden !== offscreenSubtreeIsHidden || | 2323 | | - newOffscreenSubtreeWasHidden !== offscreenSubtreeWasHidden | 2324 | | - ) { | | 2315 | + const isHidden = fiber.memoizedState !== null; | | 2316 | + const newOffscreenSubtreeIsHidden = isHidden || offscreenSubtreeIsHidden; | | 2317 | + if (newOffscreenSubtreeIsHidden) { | | 2318 | + // The Offscreen tree is hidden. Skip over its layout effects. | | 2319 | + commitLayoutMountEffects_complete(subtreeRoot, root, committedLanes); | | 2320 | + continue; | | 2321 | + } else { | | 2322 | + if ((fiber.subtreeFlags & LayoutMask) !== NoFlags) { | | 2323 | + const current = fiber.alternate; | | 2324 | + const wasHidden = current !== null && current.memoizedState !== null; | | 2325 | + const newOffscreenSubtreeWasHidden = | | 2326 | + wasHidden || offscreenSubtreeWasHidden; | 2325 | 2327 | const prevOffscreenSubtreeIsHidden = offscreenSubtreeIsHidden; | 2326 | 2328 | const prevOffscreenSubtreeWasHidden = offscreenSubtreeWasHidden; | 2327 | 2329 |
| 2328 | 2330 | // Traverse the Offscreen subtree with the current Offscreen as the root. | 2329 | 2331 | offscreenSubtreeIsHidden = newOffscreenSubtreeIsHidden; | 2330 | 2332 | offscreenSubtreeWasHidden = newOffscreenSubtreeWasHidden; | 2331 | | - commitLayoutEffects_begin( | 2332 | | - fiber, // New root; bubble back up to here and stop. | 2333 | | - root, | 2334 | | - committedLanes, | 2335 | | - ); | | 2333 | + let child = firstChild; | | 2334 | + while (child !== null) { | | 2335 | + nextEffect = child; | | 2336 | + commitLayoutEffects_begin( | | 2337 | + child, // New root; bubble back up to here and stop. | | 2338 | + root, | | 2339 | + committedLanes, | | 2340 | + ); | | 2341 | + child = child.sibling; | | 2342 | + } | 2336 | 2343 |
| 2337 | 2344 | // Restore Offscreen state and resume in our-progress traversal. | 2338 | 2345 | nextEffect = fiber; | |
Collapse file Copy file name to clipboardExpand all lines: packages/react-reconciler/src/ReactFiberCommitWork.old.js +27-20Lines changed: 27 additions & 20 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -2306,33 +2306,40 @@ function commitLayoutEffects_begin( | 2306 | 2306 | const fiber = nextEffect; | 2307 | 2307 | const firstChild = fiber.child; | 2308 | 2308 |
| 2309 | | - if (enableSuspenseLayoutEffectSemantics && isModernRoot) { | | 2309 | + if ( | | 2310 | + enableSuspenseLayoutEffectSemantics && | | 2311 | + fiber.tag === OffscreenComponent && | | 2312 | + isModernRoot | | 2313 | + ) { | 2310 | 2314 | // Keep track of the current Offscreen stack's state. | 2311 | | - if (fiber.tag === OffscreenComponent) { | 2312 | | - const current = fiber.alternate; | 2313 | | - const wasHidden = current !== null && current.memoizedState !== null; | 2314 | | - const isHidden = fiber.memoizedState !== null; | 2315 | | - | 2316 | | - const newOffscreenSubtreeIsHidden = | 2317 | | - isHidden || offscreenSubtreeIsHidden; | 2318 | | - const newOffscreenSubtreeWasHidden = | 2319 | | - wasHidden || offscreenSubtreeWasHidden; | 2320 | | - | 2321 | | - if ( | 2322 | | - newOffscreenSubtreeIsHidden !== offscreenSubtreeIsHidden || | 2323 | | - newOffscreenSubtreeWasHidden !== offscreenSubtreeWasHidden | 2324 | | - ) { | | 2315 | + const isHidden = fiber.memoizedState !== null; | | 2316 | + const newOffscreenSubtreeIsHidden = isHidden || offscreenSubtreeIsHidden; | | 2317 | + if (newOffscreenSubtreeIsHidden) { | | 2318 | + // The Offscreen tree is hidden. Skip over its layout effects. | | 2319 | + commitLayoutMountEffects_complete(subtreeRoot, root, committedLanes); | | 2320 | + continue; | | 2321 | + } else { | | 2322 | + if ((fiber.subtreeFlags & LayoutMask) !== NoFlags) { | | 2323 | + const current = fiber.alternate; | | 2324 | + const wasHidden = current !== null && current.memoizedState !== null; | | 2325 | + const newOffscreenSubtreeWasHidden = | | 2326 | + wasHidden || offscreenSubtreeWasHidden; | 2325 | 2327 | const prevOffscreenSubtreeIsHidden = offscreenSubtreeIsHidden; | 2326 | 2328 | const prevOffscreenSubtreeWasHidden = offscreenSubtreeWasHidden; | 2327 | 2329 |
| 2328 | 2330 | // Traverse the Offscreen subtree with the current Offscreen as the root. | 2329 | 2331 | offscreenSubtreeIsHidden = newOffscreenSubtreeIsHidden; | 2330 | 2332 | offscreenSubtreeWasHidden = newOffscreenSubtreeWasHidden; | 2331 | | - commitLayoutEffects_begin( | 2332 | | - fiber, // New root; bubble back up to here and stop. | 2333 | | - root, | 2334 | | - committedLanes, | 2335 | | - ); | | 2333 | + let child = firstChild; | | 2334 | + while (child !== null) { | | 2335 | + nextEffect = child; | | 2336 | + commitLayoutEffects_begin( | | 2337 | + child, // New root; bubble back up to here and stop. | | 2338 | + root, | | 2339 | + committedLanes, | | 2340 | + ); | | 2341 | + child = child.sibling; | | 2342 | + } | 2336 | 2343 |
| 2337 | 2344 | // Restore Offscreen state and resume in our-progress traversal. | 2338 | 2345 | nextEffect = fiber; | |
Collapse file Copy file name to clipboardExpand all lines: packages/react-reconciler/src/__tests__/ReactOffscreen-test.js +150Lines changed: 150 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -2,7 +2,9 @@ let React; | 2 | 2 | let ReactNoop; | 3 | 3 | let Scheduler; | 4 | 4 | let LegacyHidden; | | 5 | +let Offscreen; | 5 | 6 | let useState; | | 7 | +let useLayoutEffect; | 6 | 8 |
| 7 | 9 | describe('ReactOffscreen', () => { | 8 | 10 | beforeEach(() => { | @@ -12,7 +14,9 @@ describe('ReactOffscreen', () => { | 12 | 14 | ReactNoop = require('react-noop-renderer'); | 13 | 15 | Scheduler = require('scheduler'); | 14 | 16 | LegacyHidden = React.unstable_LegacyHidden; | | 17 | + Offscreen = React.unstable_Offscreen; | 15 | 18 | useState = React.useState; | | 19 | + useLayoutEffect = React.useLayoutEffect; | 16 | 20 | }); | 17 | 21 |
| 18 | 22 | function Text(props) { | @@ -169,4 +173,150 @@ describe('ReactOffscreen', () => { | 169 | 173 | </>, | 170 | 174 | ); | 171 | 175 | }); | | 176 | + | | 177 | + // @gate experimental | | 178 | + // @gate enableSuspenseLayoutEffectSemantics | | 179 | + it('mounts without layout effects when hidden', async () => { | | 180 | + function Child({text}) { | | 181 | + useLayoutEffect(() => { | | 182 | + Scheduler.unstable_yieldValue('Mount layout'); | | 183 | + return () => { | | 184 | + Scheduler.unstable_yieldValue('Unmount layout'); | | 185 | + }; | | 186 | + }, []); | | 187 | + return <Text text="Child" />; | | 188 | + } | | 189 | + | | 190 | + const root = ReactNoop.createRoot(); | | 191 | + | | 192 | + // Mount hidden tree. | | 193 | + await ReactNoop.act(async () => { | | 194 | + root.render( | | 195 | + <Offscreen mode="hidden"> | | 196 | + <Child /> | | 197 | + </Offscreen>, | | 198 | + ); | | 199 | + }); | | 200 | + // No layout effect. | | 201 | + expect(Scheduler).toHaveYielded(['Child']); | | 202 | + // TODO: Offscreen does not yet hide/unhide children correctly. Until we do, | | 203 | + // it should only be used inside a host component wrapper whose visibility | | 204 | + // is toggled simultaneously. | | 205 | + expect(root).toMatchRenderedOutput(<span prop="Child" />); | | 206 | + | | 207 | + // Unhide the tree. The layout effect is mounted. | | 208 | + await ReactNoop.act(async () => { | | 209 | + root.render( | | 210 | + <Offscreen mode="visible"> | | 211 | + <Child /> | | 212 | + </Offscreen>, | | 213 | + ); | | 214 | + }); | | 215 | + expect(Scheduler).toHaveYielded(['Child', 'Mount layout']); | | 216 | + expect(root).toMatchRenderedOutput(<span prop="Child" />); | | 217 | + }); | | 218 | + | | 219 | + // @gate experimental | | 220 | + // @gate enableSuspenseLayoutEffectSemantics | | 221 | + it('mounts/unmounts layout effects when visibility changes (starting visible)', async () => { | | 222 | + function Child({text}) { | | 223 | + useLayoutEffect(() => { | | 224 | + Scheduler.unstable_yieldValue('Mount layout'); | | 225 | + return () => { | | 226 | + Scheduler.unstable_yieldValue('Unmount layout'); | | 227 | + }; | | 228 | + }, []); | | 229 | + return <Text text="Child" />; | | 230 | + } | | 231 | + | | 232 | + const root = ReactNoop.createRoot(); | | 233 | + await ReactNoop.act(async () => { | | 234 | + root.render( | | 235 | + <Offscreen mode="visible"> | | 236 | + <Child /> | | 237 | + </Offscreen>, | | 238 | + ); | | 239 | + }); | | 240 | + expect(Scheduler).toHaveYielded(['Child', 'Mount layout']); | | 241 | + expect(root).toMatchRenderedOutput(<span prop="Child" />); | | 242 | + | | 243 | + // Hide the tree. The layout effect is unmounted. | | 244 | + await ReactNoop.act(async () => { | | 245 | + root.render( | | 246 | + <Offscreen mode="hidden"> | | 247 | + <Child /> | | 248 | + </Offscreen>, | | 249 | + ); | | 250 | + }); | | 251 | + expect(Scheduler).toHaveYielded(['Unmount layout', 'Child']); | | 252 | + // TODO: Offscreen does not yet hide/unhide children correctly. Until we do, | | 253 | + // it should only be used inside a host component wrapper whose visibility | | 254 | + // is toggled simultaneously. | | 255 | + expect(root).toMatchRenderedOutput(<span prop="Child" />); | | 256 | + | | 257 | + // Unhide the tree. The layout effect is re-mounted. | | 258 | + await ReactNoop.act(async () => { | | 259 | + root.render( | | 260 | + <Offscreen mode="visible"> | | 261 | + <Child /> | | 262 | + </Offscreen>, | | 263 | + ); | | 264 | + }); | | 265 | + expect(Scheduler).toHaveYielded(['Child', 'Mount layout']); | | 266 | + expect(root).toMatchRenderedOutput(<span prop="Child" />); | | 267 | + }); | | 268 | + | | 269 | + // @gate experimental | | 270 | + // @gate enableSuspenseLayoutEffectSemantics | | 271 | + it('mounts/unmounts layout effects when visibility changes (starting hidden)', async () => { | | 272 | + function Child({text}) { | | 273 | + useLayoutEffect(() => { | | 274 | + Scheduler.unstable_yieldValue('Mount layout'); | | 275 | + return () => { | | 276 | + Scheduler.unstable_yieldValue('Unmount layout'); | | 277 | + }; | | 278 | + }, []); | | 279 | + return <Text text="Child" />; | | 280 | + } | | 281 | + | | 282 | + const root = ReactNoop.createRoot(); | | 283 | + await ReactNoop.act(async () => { | | 284 | + // Start the tree hidden. The layout effect is not mounted. | | 285 | + root.render( | | 286 | + <Offscreen mode="hidden"> | | 287 | + <Child /> | | 288 | + </Offscreen>, | | 289 | + ); | | 290 | + }); | | 291 | + expect(Scheduler).toHaveYielded(['Child']); | | 292 | + // TODO: Offscreen does not yet hide/unhide children correctly. Until we do, | | 293 | + // it should only be used inside a host component wrapper whose visibility | | 294 | + // is toggled simultaneously. | | 295 | + expect(root).toMatchRenderedOutput(<span prop="Child" />); | | 296 | + | | 297 | + // Show the tree. The layout effect is mounted. | | 298 | + await ReactNoop.act(async () => { | | 299 | + root.render( | | 300 | + <Offscreen mode="visible"> | | 301 | + <Child /> | | 302 | + </Offscreen>, | | 303 | + ); | | 304 | + }); | | 305 | + expect(Scheduler).toHaveYielded(['Child', 'Mount layout']); | | 306 | + expect(root).toMatchRenderedOutput(<span prop="Child" />); | | 307 | + | | 308 | + // Hide the tree again. The layout effect is un-mounted. | | 309 | + await ReactNoop.act(async () => { | | 310 | + root.render( | | 311 | + <Offscreen mode="hidden"> | | 312 | + <Child /> | | 313 | + </Offscreen>, | | 314 | + ); | | 315 | + }); | | 316 | + expect(Scheduler).toHaveYielded(['Unmount layout', 'Child']); | | 317 | + // TODO: Offscreen does not yet hide/unhide children correctly. Until we do, | | 318 | + // it should only be used inside a host component wrapper whose visibility | | 319 | + // is toggled simultaneously. | | 320 | + expect(root).toMatchRenderedOutput(<span prop="Child" />); | | 321 | + }); | 172 | 322 | }); |
Collapse file Copy file name to clipboardExpand all lines: packages/react/index.classic.fb.js +1Lines changed: 1 addition & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -34,6 +34,7 @@ export { | 34 | 34 | unstable_Cache, | 35 | 35 | unstable_DebugTracingMode, | 36 | 36 | unstable_LegacyHidden, | | 37 | + unstable_Offscreen, | 37 | 38 | unstable_Scope, | 38 | 39 | unstable_getCacheForType, | 39 | 40 | unstable_useCacheRefresh, | |
Collapse file Copy file name to clipboardExpand all lines: packages/react/index.experimental.js +1Lines changed: 1 addition & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -31,6 +31,7 @@ export { | 31 | 31 | unstable_Cache, | 32 | 32 | unstable_DebugTracingMode, | 33 | 33 | unstable_LegacyHidden, | | 34 | + unstable_Offscreen, | 34 | 35 | unstable_getCacheForType, | 35 | 36 | unstable_useCacheRefresh, | 36 | 37 | unstable_useOpaqueIdentifier, | |
Collapse file Copy file name to clipboardExpand all lines: packages/react/index.js +1Lines changed: 1 addition & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -55,6 +55,7 @@ export { | 55 | 55 | unstable_Cache, | 56 | 56 | unstable_DebugTracingMode, | 57 | 57 | unstable_LegacyHidden, | | 58 | + unstable_Offscreen, | 58 | 59 | unstable_Scope, | 59 | 60 | unstable_getCacheForType, | 60 | 61 | unstable_useCacheRefresh, | |
Collapse file Copy file name to clipboardExpand all lines: packages/react/index.modern.fb.js +1Lines changed: 1 addition & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -33,6 +33,7 @@ export { | 33 | 33 | unstable_Cache, | 34 | 34 | unstable_DebugTracingMode, | 35 | 35 | unstable_LegacyHidden, | | 36 | + unstable_Offscreen, | 36 | 37 | unstable_Scope, | 37 | 38 | unstable_getCacheForType, | 38 | 39 | unstable_useCacheRefresh, | |
Collapse file Copy file name to clipboardExpand all lines: packages/react/src/React.js +2Lines changed: 2 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -16,6 +16,7 @@ import { | 16 | 16 | REACT_SUSPENSE_TYPE, | 17 | 17 | REACT_SUSPENSE_LIST_TYPE, | 18 | 18 | REACT_LEGACY_HIDDEN_TYPE, | | 19 | + REACT_OFFSCREEN_TYPE, | 19 | 20 | REACT_SCOPE_TYPE, | 20 | 21 | REACT_CACHE_TYPE, | 21 | 22 | } from 'shared/ReactSymbols'; | @@ -112,6 +113,7 @@ export { | 112 | 113 | useDeferredValue, | 113 | 114 | REACT_SUSPENSE_LIST_TYPE as SuspenseList, | 114 | 115 | REACT_LEGACY_HIDDEN_TYPE as unstable_LegacyHidden, | | 116 | + REACT_OFFSCREEN_TYPE as unstable_Offscreen, | 115 | 117 | getCacheForType as unstable_getCacheForType, | 116 | 118 | useCacheRefresh as unstable_useCacheRefresh, | 117 | 119 | REACT_CACHE_TYPE as unstable_Cache, | |
Collapse file Copy file name to clipboardExpand all lines: packages/shared/isValidElementType.js +2Lines changed: 2 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -21,6 +21,7 @@ import { | 21 | 21 | REACT_LAZY_TYPE, | 22 | 22 | REACT_SCOPE_TYPE, | 23 | 23 | REACT_LEGACY_HIDDEN_TYPE, | | 24 | + REACT_OFFSCREEN_TYPE, | 24 | 25 | REACT_CACHE_TYPE, | 25 | 26 | } from 'shared/ReactSymbols'; | 26 | 27 | import {enableScopeAPI, enableCache} from './ReactFeatureFlags'; | @@ -44,6 +45,7 @@ export default function isValidElementType(type: mixed) { | 44 | 45 | type === REACT_SUSPENSE_TYPE || | 45 | 46 | type === REACT_SUSPENSE_LIST_TYPE || | 46 | 47 | type === REACT_LEGACY_HIDDEN_TYPE || | | 48 | + type === REACT_OFFSCREEN_TYPE || | 47 | 49 | (enableScopeAPI && type === REACT_SCOPE_TYPE) || | 48 | 50 | (enableCache && type === REACT_CACHE_TYPE) | 49 | 51 | ) { | |
|