GeezerVison is showing github.com — Mark root as already hydrated after committing (#16739) · react/react@b0a8a3e
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__/ReactServerRenderingHydration-test.js +15Lines changed: 15 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -502,6 +502,21 @@ describe('ReactDOMServerHydration', () => { | 502 | 502 | expect(element.textContent).toBe('Hello world'); | 503 | 503 | }); | 504 | 504 |
| | 505 | + it('does not re-enter hydration after committing the first one', () => { | | 506 | + let finalHTML = ReactDOMServer.renderToString(<div />); | | 507 | + let container = document.createElement('div'); | | 508 | + container.innerHTML = finalHTML; | | 509 | + let root = ReactDOM.unstable_createRoot(container, {hydrate: true}); | | 510 | + root.render(<div />); | | 511 | + Scheduler.unstable_flushAll(); | | 512 | + root.render(null); | | 513 | + Scheduler.unstable_flushAll(); | | 514 | + // This should not reenter hydration state and therefore not trigger hydration | | 515 | + // warnings. | | 516 | + root.render(<div />); | | 517 | + Scheduler.unstable_flushAll(); | | 518 | + }); | | 519 | + | 505 | 520 | it('does not invoke an event on a concurrent hydrating node until it commits', () => { | 506 | 521 | function Sibling({text}) { | 507 | 522 | Scheduler.unstable_yieldValue('Sibling'); | |
Collapse file Copy file name to clipboardExpand all lines: packages/react-reconciler/src/ReactFiberBeginWork.js +1-8Lines changed: 1 addition & 8 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -937,14 +937,7 @@ function updateHostRoot(current, workInProgress, renderExpirationTime) { | 937 | 937 | ); | 938 | 938 | } | 939 | 939 | const root: FiberRoot = workInProgress.stateNode; | 940 | | - if ( | 941 | | - // TODO: This is a bug because if we render null after having hydrating, | 942 | | - // we'll reenter hydration state at the next update which will then | 943 | | - // trigger hydration warnings. | 944 | | - (current === null || current.child === null) && | 945 | | - root.hydrate && | 946 | | - enterHydrationState(workInProgress) | 947 | | - ) { | | 940 | + if (root.hydrate && enterHydrationState(workInProgress)) { | 948 | 941 | // If we don't have any current children this might be the first pass. | 949 | 942 | // We always try to hydrate. If this isn't a hydration pass there won't | 950 | 943 | // be any children to hydrate which is effectively the same thing as | |
Collapse file Copy file name to clipboardExpand all lines: packages/react-reconciler/src/ReactFiberCommitWork.js +18Lines changed: 18 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -81,6 +81,7 @@ import { | 81 | 81 | getPublicInstance, | 82 | 82 | supportsMutation, | 83 | 83 | supportsPersistence, | | 84 | + supportsHydration, | 84 | 85 | commitMount, | 85 | 86 | commitUpdate, | 86 | 87 | resetTextContent, | @@ -1297,6 +1298,16 @@ function commitWork(current: Fiber | null, finishedWork: Fiber): void { | 1297 | 1298 | attachSuspenseRetryListeners(finishedWork); | 1298 | 1299 | return; | 1299 | 1300 | } | | 1301 | + case HostRoot: { | | 1302 | + const root: FiberRoot = finishedWork.stateNode; | | 1303 | + if (supportsHydration) { | | 1304 | + if (root.hydrate) { | | 1305 | + // We've just hydrated. No need to hydrate again. | | 1306 | + root.hydrate = false; | | 1307 | + } | | 1308 | + } | | 1309 | + break; | | 1310 | + } | 1300 | 1311 | } | 1301 | 1312 |
| 1302 | 1313 | commitContainer(finishedWork); | @@ -1366,6 +1377,13 @@ function commitWork(current: Fiber | null, finishedWork: Fiber): void { | 1366 | 1377 | return; | 1367 | 1378 | } | 1368 | 1379 | case HostRoot: { | | 1380 | + const root: FiberRoot = finishedWork.stateNode; | | 1381 | + if (supportsHydration) { | | 1382 | + if (root.hydrate) { | | 1383 | + // We've just hydrated. No need to hydrate again. | | 1384 | + root.hydrate = false; | | 1385 | + } | | 1386 | + } | 1369 | 1387 | return; | 1370 | 1388 | } | 1371 | 1389 | case Profiler: { | |
Collapse file Copy file name to clipboardExpand all lines: packages/react-reconciler/src/ReactFiberCompleteWork.js +6-1Lines changed: 6 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -662,7 +662,12 @@ function completeWork( | 662 | 662 | if (current === null || current.child === null) { | 663 | 663 | // If we hydrated, pop so that we can delete any remaining children | 664 | 664 | // that weren't hydrated. | 665 | | - popHydrationState(workInProgress); | | 665 | + let wasHydrated = popHydrationState(workInProgress); | | 666 | + if (wasHydrated) { | | 667 | + // If we hydrated, then we'll need to schedule an update for | | 668 | + // the commit side-effects on the root. | | 669 | + markUpdate(workInProgress); | | 670 | + } | 666 | 671 | } | 667 | 672 | updateHostContainer(workInProgress); | 668 | 673 | break; | |
|