mirror of
https://github.com/facebook/react.git
synced 2025-11-01 09:12:30 +00:00
376d5c1b5a
Some of our internal reconciler types have leaked into other packages. Usually, these types are treated as opaque; we don't read and write to its fields. This is good. However, the type is often passed back to a reconciler method. For example, React DOM creates a FiberRoot with `createContainer`, then passes that root to `updateContainer`. It doesn't do anything with the root except pass it through, but because `updateContainer` expects a full FiberRoot, React DOM is still coupled to all its fields. I don't know if there's an idiomatic way to handle this in Flow. Opaque types are simlar, but those only work within a single file. AFAIK, there's no way to use a package as the boundary for opaqueness. The immediate problem this presents is that the reconciler refactor will involve changes to our internal data structures. I don't want to have to fork every single package that happens to pass through a Fiber or FiberRoot, or access any one of its fields. So my current plan is to share the same Flow type across both forks. The shared type will be a superset of each implementation's type, e.g. Fiber will have both an `expirationTime` field and a `lanes` field. The implementations will diverge, but not the types. To do this, I lifted the type definitions into a separate module.
209 lines
6.2 KiB
JavaScript
209 lines
6.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
|
|
*/
|
|
|
|
import type {Container} from './ReactDOMHostConfig';
|
|
import type {RootTag} from 'react-reconciler/src/ReactRootTags';
|
|
import type {ReactNodeList} from 'shared/ReactTypes';
|
|
import type {FiberRoot} from 'react-reconciler/src/ReactInternalTypes';
|
|
import {findHostInstanceWithNoPortals} from 'react-reconciler/src/ReactFiberReconciler';
|
|
|
|
export type RootType = {
|
|
render(children: ReactNodeList): void,
|
|
unmount(): void,
|
|
_internalRoot: FiberRoot,
|
|
...
|
|
};
|
|
|
|
export type RootOptions = {
|
|
hydrate?: boolean,
|
|
hydrationOptions?: {
|
|
onHydrated?: (suspenseNode: Comment) => void,
|
|
onDeleted?: (suspenseNode: Comment) => void,
|
|
...
|
|
},
|
|
...
|
|
};
|
|
|
|
import {
|
|
isContainerMarkedAsRoot,
|
|
markContainerAsRoot,
|
|
unmarkContainerAsRoot,
|
|
} from './ReactDOMComponentTree';
|
|
import {eagerlyTrapReplayableEvents} from '../events/ReactDOMEventReplaying';
|
|
import {
|
|
ELEMENT_NODE,
|
|
COMMENT_NODE,
|
|
DOCUMENT_NODE,
|
|
DOCUMENT_FRAGMENT_NODE,
|
|
} from '../shared/HTMLNodeType';
|
|
|
|
import {
|
|
createContainer,
|
|
updateContainer,
|
|
} from 'react-reconciler/src/ReactFiberReconciler';
|
|
import invariant from 'shared/invariant';
|
|
import {
|
|
BlockingRoot,
|
|
ConcurrentRoot,
|
|
LegacyRoot,
|
|
} from 'react-reconciler/src/ReactRootTags';
|
|
|
|
function ReactDOMRoot(container: Container, options: void | RootOptions) {
|
|
this._internalRoot = createRootImpl(container, ConcurrentRoot, options);
|
|
}
|
|
|
|
function ReactDOMBlockingRoot(
|
|
container: Container,
|
|
tag: RootTag,
|
|
options: void | RootOptions,
|
|
) {
|
|
this._internalRoot = createRootImpl(container, tag, options);
|
|
}
|
|
|
|
ReactDOMRoot.prototype.render = ReactDOMBlockingRoot.prototype.render = function(
|
|
children: ReactNodeList,
|
|
): void {
|
|
const root = this._internalRoot;
|
|
if (__DEV__) {
|
|
if (typeof arguments[1] === 'function') {
|
|
console.error(
|
|
'render(...): does not support the second callback argument. ' +
|
|
'To execute a side effect after rendering, declare it in a component body with useEffect().',
|
|
);
|
|
}
|
|
const container = root.containerInfo;
|
|
|
|
if (container.nodeType !== COMMENT_NODE) {
|
|
const hostInstance = findHostInstanceWithNoPortals(root.current);
|
|
if (hostInstance) {
|
|
if (hostInstance.parentNode !== container) {
|
|
console.error(
|
|
'render(...): It looks like the React-rendered content of the ' +
|
|
'root container was removed without using React. This is not ' +
|
|
'supported and will cause errors. Instead, call ' +
|
|
"root.unmount() to empty a root's container.",
|
|
);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
updateContainer(children, root, null, null);
|
|
};
|
|
|
|
ReactDOMRoot.prototype.unmount = ReactDOMBlockingRoot.prototype.unmount = function(): void {
|
|
if (__DEV__) {
|
|
if (typeof arguments[0] === 'function') {
|
|
console.error(
|
|
'unmount(...): does not support a callback argument. ' +
|
|
'To execute a side effect after rendering, declare it in a component body with useEffect().',
|
|
);
|
|
}
|
|
}
|
|
const root = this._internalRoot;
|
|
const container = root.containerInfo;
|
|
updateContainer(null, root, null, () => {
|
|
unmarkContainerAsRoot(container);
|
|
});
|
|
};
|
|
|
|
function createRootImpl(
|
|
container: Container,
|
|
tag: RootTag,
|
|
options: void | RootOptions,
|
|
) {
|
|
// Tag is either LegacyRoot or Concurrent Root
|
|
const hydrate = options != null && options.hydrate === true;
|
|
const hydrationCallbacks =
|
|
(options != null && options.hydrationOptions) || null;
|
|
const root = createContainer(container, tag, hydrate, hydrationCallbacks);
|
|
markContainerAsRoot(root.current, container);
|
|
if (hydrate && tag !== LegacyRoot) {
|
|
const doc =
|
|
container.nodeType === DOCUMENT_NODE
|
|
? container
|
|
: container.ownerDocument;
|
|
eagerlyTrapReplayableEvents(container, doc);
|
|
}
|
|
return root;
|
|
}
|
|
|
|
export function createRoot(
|
|
container: Container,
|
|
options?: RootOptions,
|
|
): RootType {
|
|
invariant(
|
|
isValidContainer(container),
|
|
'createRoot(...): Target container is not a DOM element.',
|
|
);
|
|
warnIfReactDOMContainerInDEV(container);
|
|
return new ReactDOMRoot(container, options);
|
|
}
|
|
|
|
export function createBlockingRoot(
|
|
container: Container,
|
|
options?: RootOptions,
|
|
): RootType {
|
|
invariant(
|
|
isValidContainer(container),
|
|
'createRoot(...): Target container is not a DOM element.',
|
|
);
|
|
warnIfReactDOMContainerInDEV(container);
|
|
return new ReactDOMBlockingRoot(container, BlockingRoot, options);
|
|
}
|
|
|
|
export function createLegacyRoot(
|
|
container: Container,
|
|
options?: RootOptions,
|
|
): RootType {
|
|
return new ReactDOMBlockingRoot(container, LegacyRoot, options);
|
|
}
|
|
|
|
export function isValidContainer(node: mixed): boolean {
|
|
return !!(
|
|
node &&
|
|
(node.nodeType === ELEMENT_NODE ||
|
|
node.nodeType === DOCUMENT_NODE ||
|
|
node.nodeType === DOCUMENT_FRAGMENT_NODE ||
|
|
(node.nodeType === COMMENT_NODE &&
|
|
(node: any).nodeValue === ' react-mount-point-unstable '))
|
|
);
|
|
}
|
|
|
|
function warnIfReactDOMContainerInDEV(container) {
|
|
if (__DEV__) {
|
|
if (
|
|
container.nodeType === ELEMENT_NODE &&
|
|
((container: any): Element).tagName &&
|
|
((container: any): Element).tagName.toUpperCase() === 'BODY'
|
|
) {
|
|
console.error(
|
|
'createRoot(): Creating roots directly with document.body is ' +
|
|
'discouraged, since its children are often manipulated by third-party ' +
|
|
'scripts and browser extensions. This may lead to subtle ' +
|
|
'reconciliation issues. Try using a container element created ' +
|
|
'for your app.',
|
|
);
|
|
}
|
|
if (isContainerMarkedAsRoot(container)) {
|
|
if (container._reactRootContainer) {
|
|
console.error(
|
|
'You are calling ReactDOM.createRoot() on a container that was previously ' +
|
|
'passed to ReactDOM.render(). This is not supported.',
|
|
);
|
|
} else {
|
|
console.error(
|
|
'You are calling ReactDOM.createRoot() on a container that ' +
|
|
'has already been passed to createRoot() before. Instead, call ' +
|
|
'root.render() on the existing root instead if you want to update it.',
|
|
);
|
|
}
|
|
}
|
|
}
|
|
}
|