Make sure top-level callback has correct context

Closes #5125 -- incorporates essentially the same fix with a much simpler test case. Thanks @yiminghe for reporting!
This commit is contained in:
Ben Alpert
2015-10-27 13:11:45 -07:00
parent 65b9ceb4f8
commit 7f205e9c1b
2 changed files with 48 additions and 3 deletions
+8 -3
View File
@@ -610,12 +610,17 @@ var ReactMount = {
var prevWrappedElement = prevComponent._currentElement;
var prevElement = prevWrappedElement.props;
if (shouldUpdateReactComponent(prevElement, nextElement)) {
return ReactMount._updateRootComponent(
var publicInst = prevComponent._renderedComponent.getPublicInstance();
var updatedCallback = callback && function() {
callback.call(publicInst);
};
ReactMount._updateRootComponent(
prevComponent,
nextWrappedElement,
container,
callback
)._renderedComponent.getPublicInstance();
updatedCallback
);
return publicInst;
} else {
ReactMount.unmountComponentAtNode(container);
}
@@ -299,4 +299,44 @@ describe('ReactMount', function() {
ReactDOM.render(<Component step={1} />, container);
ReactMount.getID(container.querySelector('a'));
});
it('passes the correct callback context', function() {
var container = document.createElement('div');
var calls = 0;
ReactDOM.render(<div />, container, function() {
expect(this.nodeName).toBe('DIV');
calls++;
});
// Update, no type change
ReactDOM.render(<div />, container, function() {
expect(this.nodeName).toBe('DIV');
calls++;
});
// Update, type change
ReactDOM.render(<span />, container, function() {
expect(this.nodeName).toBe('SPAN');
calls++;
});
// Batched update, no type change
ReactDOM.unstable_batchedUpdates(function() {
ReactDOM.render(<span />, container, function() {
expect(this.nodeName).toBe('SPAN');
calls++;
});
});
// Batched update, type change
ReactDOM.unstable_batchedUpdates(function() {
ReactDOM.render(<article />, container, function() {
expect(this.nodeName).toBe('ARTICLE');
calls++;
});
});
expect(calls).toBe(5);
});
});