Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mark root as already hydrated after committing #16739

Merged
merged 2 commits into from
Sep 11, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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