Skip to content

Commit

Permalink
ReactDOM.createRoot
Browse files Browse the repository at this point in the history
Introduce new API for creating roots. Only root.render and root.unmount
are implemented. Later we'll add root.prerender, and support for lazy
roots (roots with DOM containers that resolve lazily).
  • Loading branch information
acdlite committed Oct 14, 2017
1 parent 3ffb5d0 commit 1bb13d5
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 0 deletions.
32 changes: 32 additions & 0 deletions src/renderers/dom/fiber/ReactDOMFiberEntry.js
Original file line number Diff line number Diff line change
Expand Up @@ -757,7 +757,39 @@ function createPortal(
return ReactPortal.createPortal(children, container, null, key);
}

type ReactRootNode = {
render(children: ReactNodeList, callback: ?() => mixed): void,
unmount(callback: ?() => mixed): void,

_reactRootContainer: *,
};

type RootOptions = {
hydrate?: boolean,
};

function ReactRoot(container: Container, hydrate: boolean) {
const root = DOMRenderer.createContainer(container);
this._reactRootContainer = root;
}
ReactRoot.prototype.render = function(
children: ReactNodeList,
callback: ?() => mixed,
): void {
const root = this._reactRootContainer;
DOMRenderer.updateContainer(children, root, null, callback);
};
ReactRoot.prototype.unmount = function(callback) {
const root = this._reactRootContainer;
DOMRenderer.updateContainer(null, root, null, callback);
};

var ReactDOMFiber = {
createRoot(container: DOMContainer, options?: RootOptions): ReactRootNode {
const hydrate = options != null && options.hydrate === true;
return new ReactRoot(container, hydrate);
},

createPortal,

findDOMNode(
Expand Down
35 changes: 35 additions & 0 deletions src/renderers/dom/shared/__tests__/ReactDOMRoot-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/**
* Copyright (c) 2013-present, Facebook, Inc.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @emails react-core
*/

'use strict';

var React = require('react');
var ReactDOM = require('react-dom');

describe('ReactDOMRoot', () => {
let container;

beforeEach(() => {
container = document.createElement('div');
});

it('renders children', () => {
const root = ReactDOM.createRoot(container);
root.render(<div>Hi</div>);
expect(container.textContent).toEqual('Hi');
});

it('unmounts children', () => {
const root = ReactDOM.createRoot(container);
root.render(<div>Hi</div>);
expect(container.textContent).toEqual('Hi');
root.unmount();
expect(container.textContent).toEqual('');
});
});

0 comments on commit 1bb13d5

Please sign in to comment.