Skip to content

Fix the context handling when updating a rendered component. #3494

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 25, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/core/ReactCompositeComponent.js
Original file line number Diff line number Diff line change
Expand Up @@ -743,7 +743,7 @@ var ReactCompositeComponentMixin = {
this._renderedComponent,
thisID,
transaction,
context
this._processChildContext(context)
);
this._replaceNodeWithMarkupByID(prevComponentID, nextMarkup);
}
Expand Down
60 changes: 60 additions & 0 deletions src/core/__tests__/ReactCompositeComponent-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ var ReactMount;
var ReactPropTypes;
var ReactServerRendering;
var ReactTestUtils;
var ReactUpdates;

var reactComponentExpect;
var mocks;
Expand All @@ -37,6 +38,7 @@ describe('ReactCompositeComponent', function() {
ReactTestUtils = require('ReactTestUtils');
ReactMount = require('ReactMount');
ReactServerRendering = require('ReactServerRendering');
ReactUpdates = require('ReactUpdates');

MorphingComponent = React.createClass({
getInitialState: function() {
Expand Down Expand Up @@ -561,6 +563,64 @@ describe('ReactCompositeComponent', function() {
reactComponentExpect(grandchildInstance).scalarContextEqual({foo: 'bar', depth: 1});
});

it('should pass context when re-rendered', function() {
var parentInstance = null;
var childInstance = null;

var Parent = React.createClass({
childContextTypes: {
foo: ReactPropTypes.string,
depth: ReactPropTypes.number
},

getChildContext: function() {
return {
foo: 'bar',
depth: 0
};
},

getInitialState: function() {
return {
flag: false
}
},

render: function() {
var output = <Child />;
if (!this.state.flag) {
output = <span>Child</span>;
}
return output;
}
});

var Child = React.createClass({
contextTypes: {
foo: ReactPropTypes.string,
depth: ReactPropTypes.number
},

render: function() {
childInstance = this;
return <span>Child</span>;
}
});

parentInstance = ReactTestUtils.renderIntoDocument(<Parent />);
expect(childInstance).toBeNull();

expect(parentInstance.state.flag).toBe(false);
ReactUpdates.batchedUpdates(function() {
parentInstance.setState({flag: true});
});
expect(parentInstance.state.flag).toBe(true);

expect(console.warn.argsForCall.length).toBe(0);

reactComponentExpect(childInstance).scalarContextEqual({foo: 'bar', depth: 0});
});

it('warn if context keys differ', function() {
var Component = React.createClass({
contextTypes: {
Expand Down