Preserve DOM node when updating empty component

Fixes #2770.
This commit is contained in:
Ben Alpert committed 2015-09-09 14:00:59 -07:00
1 parent 999b0f9b3e
commit db589a7175
3 files changed
+39 -13

No files matched your search

@@ -90,7 +90,8 @@ var ReactChildReconciler = {
var prevChild = prevChildren && prevChildren[name];
var prevElement = prevChild && prevChild._currentElement;
var nextElement = nextChildren[name];
if (shouldUpdateReactComponent(prevElement, nextElement)) {
if (prevChild != null &&
shouldUpdateReactComponent(prevElement, nextElement)) {
ReactReconciler.receiveComponent(
prevChild, nextElement, transaction, context
);
@@ -265,4 +265,25 @@ describe('ReactEmptyComponent', function() {
ReactTestUtils.renderIntoDocument(<Parent />);
}).not.toThrow();
});
it('preserves the dom node during updates', function() {
var Empty = React.createClass({
render: function() {
return null;
},
});
var container = document.createElement('div');
ReactDOM.render(<Empty />, container);
var noscript1 = container.firstChild;
expect(noscript1.tagName).toBe('NOSCRIPT');
// This update shouldn't create a DOM node
ReactDOM.render(<Empty />, container);
var noscript2 = container.firstChild;
expect(noscript2.tagName).toBe('NOSCRIPT');
expect(noscript1).toBe(noscript2);
});
});
@@ -24,18 +24,22 @@
* @protected
*/
function shouldUpdateReactComponent(prevElement, nextElement) {
if (prevElement != null && nextElement != null) {
var prevType = typeof prevElement;
var nextType = typeof nextElement;
if (prevType === 'string' || prevType === 'number') {
return (nextType === 'string' || nextType === 'number');
} else {
return (
nextType === 'object' &&
prevElement.type === nextElement.type &&
prevElement.key === nextElement.key
);
}
var prevEmpty = prevElement === null || prevElement === false;
var nextEmpty = nextElement === null || nextElement === false;
if (prevEmpty || nextEmpty) {
return prevEmpty === nextEmpty;
}
var prevType = typeof prevElement;
var nextType = typeof nextElement;
if (prevType === 'string' || prevType === 'number') {
return (nextType === 'string' || nextType === 'number');
} else {
return (
nextType === 'object' &&
prevElement.type === nextElement.type &&
prevElement.key === nextElement.key
);
}
return false;
}