mirror of
https://github.com/zebrajr/react.git
synced 2025-12-07 12:20:38 +01:00
* Support Document as a container for hydration and rendering Previously Document was not handled effectively as a container. in particual when hydrating if there was a fallback to client rendering React would attempt to append a new <html> element into the document before clearing out the existing one which errored leaving the application in brokent state. The initial approach I took was to recycle the documentElement and never remove or append it, always just moving it to the right fiber and appending the right children (heady/body) as needed. However in testing a simple approach in modern browsers it seems like treating the documentElement like any other element works fine. This change modifies the clearContainer method to remove the documentElement if the container is a DOCUMENT_NODE. Once the container is cleared React can append a new documentElement via normal means. * Allow Document as container for createRoot previously rendering into Document was broken and only hydration worked because React did not properly deal with the documentElement and would error in a broken state if used that way. With the previous commit addressing this limitation this change re-adds Document as a valid container for createRoot. It should be noted that if you use document with createRoot it will drop anything a 3rd party scripts adds the page before rendering for the first time.
57 lines
1.2 KiB
JavaScript
57 lines
1.2 KiB
JavaScript
/**
|
|
* Copyright (c) Facebook, Inc. and its affiliates.
|
|
*
|
|
* This source code is licensed under the MIT license found in the
|
|
* LICENSE file in the root directory of this source tree.
|
|
*
|
|
* @flow
|
|
*/
|
|
|
|
'use strict';
|
|
|
|
import type {ReactNodeList} from 'shared/ReactTypes';
|
|
import type {
|
|
RootType,
|
|
HydrateRootOptions,
|
|
CreateRootOptions,
|
|
} from './src/client/ReactDOMRoot';
|
|
|
|
import {
|
|
createRoot as createRootImpl,
|
|
hydrateRoot as hydrateRootImpl,
|
|
__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED as Internals,
|
|
} from './';
|
|
|
|
export function createRoot(
|
|
container: Element | Document | DocumentFragment,
|
|
options?: CreateRootOptions,
|
|
): RootType {
|
|
if (__DEV__) {
|
|
Internals.usingClientEntryPoint = true;
|
|
}
|
|
try {
|
|
return createRootImpl(container, options);
|
|
} finally {
|
|
if (__DEV__) {
|
|
Internals.usingClientEntryPoint = false;
|
|
}
|
|
}
|
|
}
|
|
|
|
export function hydrateRoot(
|
|
container: Document | Element,
|
|
children: ReactNodeList,
|
|
options?: HydrateRootOptions,
|
|
): RootType {
|
|
if (__DEV__) {
|
|
Internals.usingClientEntryPoint = true;
|
|
}
|
|
try {
|
|
return hydrateRootImpl(container, children, options);
|
|
} finally {
|
|
if (__DEV__) {
|
|
Internals.usingClientEntryPoint = false;
|
|
}
|
|
}
|
|
}
|