Skip to content

Commit

Permalink
Mark root as already hydrated after committing (#16739)
Browse files Browse the repository at this point in the history
* Mark root as already hydrated after committing

* Remove current/child check for hydration and rely on the root flag instead
  • Loading branch information
sebmarkbage committed Sep 11, 2019
1 parent e04f425 commit b0a8a3e
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 9 deletions.
Expand Up @@ -502,6 +502,21 @@ describe('ReactDOMServerHydration', () => {
expect(element.textContent).toBe('Hello world');
});

it('does not re-enter hydration after committing the first one', () => {
let finalHTML = ReactDOMServer.renderToString(<div />);
let container = document.createElement('div');
container.innerHTML = finalHTML;
let root = ReactDOM.unstable_createRoot(container, {hydrate: true});
root.render(<div />);
Scheduler.unstable_flushAll();
root.render(null);
Scheduler.unstable_flushAll();
// This should not reenter hydration state and therefore not trigger hydration
// warnings.
root.render(<div />);
Scheduler.unstable_flushAll();
});

it('does not invoke an event on a concurrent hydrating node until it commits', () => {
function Sibling({text}) {
Scheduler.unstable_yieldValue('Sibling');
Expand Down
9 changes: 1 addition & 8 deletions packages/react-reconciler/src/ReactFiberBeginWork.js
Expand Up @@ -937,14 +937,7 @@ function updateHostRoot(current, workInProgress, renderExpirationTime) {
);
}
const root: FiberRoot = workInProgress.stateNode;
if (
// TODO: This is a bug because if we render null after having hydrating,
// we'll reenter hydration state at the next update which will then
// trigger hydration warnings.
(current === null || current.child === null) &&
root.hydrate &&
enterHydrationState(workInProgress)
) {
if (root.hydrate && enterHydrationState(workInProgress)) {
// If we don't have any current children this might be the first pass.
// We always try to hydrate. If this isn't a hydration pass there won't
// be any children to hydrate which is effectively the same thing as
Expand Down
18 changes: 18 additions & 0 deletions packages/react-reconciler/src/ReactFiberCommitWork.js
Expand Up @@ -81,6 +81,7 @@ import {
getPublicInstance,
supportsMutation,
supportsPersistence,
supportsHydration,
commitMount,
commitUpdate,
resetTextContent,
Expand Down Expand Up @@ -1297,6 +1298,16 @@ function commitWork(current: Fiber | null, finishedWork: Fiber): void {
attachSuspenseRetryListeners(finishedWork);
return;
}
case HostRoot: {
const root: FiberRoot = finishedWork.stateNode;
if (supportsHydration) {
if (root.hydrate) {
// We've just hydrated. No need to hydrate again.
root.hydrate = false;
}
}
break;
}
}

commitContainer(finishedWork);
Expand Down Expand Up @@ -1366,6 +1377,13 @@ function commitWork(current: Fiber | null, finishedWork: Fiber): void {
return;
}
case HostRoot: {
const root: FiberRoot = finishedWork.stateNode;
if (supportsHydration) {
if (root.hydrate) {
// We've just hydrated. No need to hydrate again.
root.hydrate = false;
}
}
return;
}
case Profiler: {
Expand Down
7 changes: 6 additions & 1 deletion packages/react-reconciler/src/ReactFiberCompleteWork.js
Expand Up @@ -662,7 +662,12 @@ function completeWork(
if (current === null || current.child === null) {
// If we hydrated, pop so that we can delete any remaining children
// that weren't hydrated.
popHydrationState(workInProgress);
let wasHydrated = popHydrationState(workInProgress);
if (wasHydrated) {
// If we hydrated, then we'll need to schedule an update for
// the commit side-effects on the root.
markUpdate(workInProgress);
}
}
updateHostContainer(workInProgress);
break;
Expand Down

0 comments on commit b0a8a3e

Please sign in to comment.