Skip to content

Commit 100af48

Browse files
jeffmozpao
authored andcommitted
Support rendering different components into same node
var container = ...; // some DOM node React.renderComponent(<div />, container); React.renderComponent(<span />, container); This should replace the rendered <div> with a <span>, effectively reconciling at the root level.
1 parent 3e211bf commit 100af48

File tree

2 files changed

+31
-5
lines changed

2 files changed

+31
-5
lines changed

src/core/ReactMount.js

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -106,11 +106,15 @@ var ReactMount = {
106106
renderComponent: function(nextComponent, container) {
107107
var prevComponent = instanceByReactRootID[getReactRootID(container)];
108108
if (prevComponent) {
109-
var nextProps = nextComponent.props;
110-
ReactMount.scrollMonitor(container, function() {
111-
prevComponent.replaceProps(nextProps);
112-
});
113-
return prevComponent;
109+
if (prevComponent.constructor === nextComponent.constructor) {
110+
var nextProps = nextComponent.props;
111+
ReactMount.scrollMonitor(container, function() {
112+
prevComponent.replaceProps(nextProps);
113+
});
114+
return prevComponent;
115+
} else {
116+
ReactMount.unmountAndReleaseReactRootNode(container);
117+
}
114118
}
115119

116120
ReactMount.prepareTopLevelEvents(ReactEventTopLevelCallback);

src/core/__tests__/ReactMount-test.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/**
2+
* @jsx React.DOM
3+
* @emails react-core
4+
*/
5+
6+
"use strict";
7+
8+
describe('ReactMount', function() {
9+
var React = require('React');
10+
var ReactMount = require('ReactMount');
11+
12+
it('should render different components in same root', function() {
13+
var container = document.createElement('container');
14+
document.documentElement.appendChild(container);
15+
16+
ReactMount.renderComponent(<div></div>, container);
17+
expect(container.firstChild.nodeName).toBe('DIV');
18+
19+
ReactMount.renderComponent(<span></span>, container);
20+
expect(container.firstChild.nodeName).toBe('SPAN');
21+
});
22+
});

0 commit comments

Comments
 (0)