UNPKG

2.77 kBJavaScriptView Raw
1/**
2 * Copyright (c) 2013-present, Facebook, Inc.
3 *
4 * This source code is licensed under the MIT license found in the
5 * LICENSE file in the root directory of this source tree.
6 *
7 *
8 */
9
10'use strict';
11
12var ReactFiberReconciler = require('./ReactFiberReconciler');
13
14var warning = require('fbjs/lib/warning');
15
16function recursivelyAppendChildren(parent, child) {
17 if (!child) {
18 return;
19 }
20 /* $FlowFixMe: Element should have this property. */
21 if (child.nodeType === 1) {
22 /* $FlowFixMe: Refinement issue. I don't know how to express different. */
23 parent.appendChild(child);
24 } else {
25 /* As a result of the refinement issue this type isn't known. */
26 var node = child;
27 do {
28 recursivelyAppendChildren(parent, node.output);
29 } while (node = node.sibling);
30 }
31}
32
33var DOMRenderer = ReactFiberReconciler({
34 updateContainer: function (container, children) {
35 container.innerHTML = '';
36 recursivelyAppendChildren(container, children);
37 },
38 createInstance: function (type, props, children) {
39 var domElement = document.createElement(type);
40 recursivelyAppendChildren(domElement, children);
41 if (typeof props.children === 'string') {
42 domElement.textContent = props.children;
43 }
44 return domElement;
45 },
46 prepareUpdate: function (domElement, oldProps, newProps, children) {
47 return true;
48 },
49 commitUpdate: function (domElement, oldProps, newProps, children) {
50 domElement.innerHTML = '';
51 recursivelyAppendChildren(domElement, children);
52 if (typeof newProps.children === 'string') {
53 domElement.textContent = newProps.children;
54 }
55 },
56 deleteInstance: function (instance) {
57 // Noop
58 },
59
60
61 scheduleAnimationCallback: window.requestAnimationFrame,
62
63 scheduleDeferredCallback: window.requestIdleCallback
64});
65
66var warned = false;
67
68function warnAboutUnstableUse() {
69 process.env.NODE_ENV !== 'production' ? warning(warned, 'You are using React DOM Fiber which is an experimental renderer. ' + 'It is likely to have bugs, breaking changes and is unsupported.') : void 0;
70 warned = true;
71}
72
73var ReactDOM = {
74 render: function (element, container) {
75 warnAboutUnstableUse();
76 if (!container._reactRootContainer) {
77 container._reactRootContainer = DOMRenderer.mountContainer(element, container);
78 } else {
79 DOMRenderer.updateContainer(element, container._reactRootContainer);
80 }
81 },
82 unmountComponentAtNode: function (container) {
83 warnAboutUnstableUse();
84 var root = container._reactRootContainer;
85 if (root) {
86 // TODO: Is it safe to reset this now or should I wait since this
87 // unmount could be deferred?
88 container._reactRootContainer = null;
89 DOMRenderer.unmountContainer(root);
90 }
91 }
92};
93
94module.exports = ReactDOM;
\No newline at end of file