Skip to content
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

Fix renderSubtreeIntoContainer to update context #7125

Merged
merged 4 commits into from Jun 27, 2016
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
105 changes: 104 additions & 1 deletion src/addons/__tests__/renderSubtreeIntoContainer-test.js
Expand Up @@ -12,13 +12,13 @@
'use strict';

var React = require('React');
var ReactDOM = require('ReactDOM');
var ReactTestUtils = require('ReactTestUtils');
var renderSubtreeIntoContainer = require('renderSubtreeIntoContainer');

describe('renderSubtreeIntoContainer', function() {

it('should pass context when rendering subtree elsewhere', function() {

var portal = document.createElement('div');

var Component = React.createClass({
Expand Down Expand Up @@ -92,4 +92,107 @@ describe('renderSubtreeIntoContainer', function() {
},
});
});

it('should update context if it changes due to setState', function() {
var container = document.createElement('div');
document.body.appendChild(container);
var portal = document.createElement('div');

var Component = React.createClass({
contextTypes: {
foo: React.PropTypes.string.isRequired,
getFoo: React.PropTypes.func.isRequired,
},

render: function() {
return <div>{this.context.foo + '-' + this.context.getFoo()}</div>;
},
});

var Parent = React.createClass({
childContextTypes: {
foo: React.PropTypes.string.isRequired,
getFoo: React.PropTypes.func.isRequired,
},

getChildContext: function() {
return {
foo: this.state.bar,
getFoo: () => this.state.bar,
};
},

getInitialState: function() {
return {
bar: 'initial',
};
},

render: function() {
return null;
},

componentDidMount: function() {
renderSubtreeIntoContainer(this, <Component />, portal);
},

componentDidUpdate() {
renderSubtreeIntoContainer(this, <Component />, portal);
},
});

var instance = ReactDOM.render(<Parent />, container);
expect(portal.firstChild.innerHTML).toBe('initial-initial');
instance.setState({bar: 'changed'});
expect(portal.firstChild.innerHTML).toBe('changed-changed');
});

it('should update context if it changes due to re-render', function() {
var container = document.createElement('div');
document.body.appendChild(container);
var portal = document.createElement('div');

var Component = React.createClass({
contextTypes: {
foo: React.PropTypes.string.isRequired,
getFoo: React.PropTypes.func.isRequired,
},

render: function() {
return <div>{this.context.foo + '-' + this.context.getFoo()}</div>;
},
});

var Parent = React.createClass({
childContextTypes: {
foo: React.PropTypes.string.isRequired,
getFoo: React.PropTypes.func.isRequired,
},

getChildContext: function() {
return {
foo: this.props.bar,
getFoo: () => this.props.bar,
};
},

render: function() {
return null;
},

componentDidMount: function() {
renderSubtreeIntoContainer(this, <Component />, portal);
},

componentDidUpdate() {
renderSubtreeIntoContainer(this, <Component />, portal);
},
});

ReactDOM.render(<Parent bar="initial" />, container);
expect(portal.firstChild.innerHTML).toBe('initial-initial');
ReactDOM.render(<Parent bar="changed" />, container);
expect(portal.firstChild.innerHTML).toBe('changed-changed');
});

});
14 changes: 8 additions & 6 deletions src/renderers/dom/client/ReactMount.js
Expand Up @@ -287,10 +287,11 @@ var ReactMount = {
_updateRootComponent: function(
prevComponent,
nextElement,
nextContext,
container,
callback) {
ReactMount.scrollMonitor(container, function() {
ReactUpdateQueue.enqueueElementInternal(prevComponent, nextElement);
ReactUpdateQueue.enqueueElementInternal(prevComponent, nextElement, nextContext);
if (callback) {
ReactUpdateQueue.enqueueCallbackInternal(prevComponent, callback);
}
Expand Down Expand Up @@ -439,6 +440,10 @@ var ReactMount = {
null,
nextElement
);
var nextContext = parentComponent ?
parentComponent._reactInternalInstance._processChildContext(
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mr. Ugly but that’s exactly what we’ve been doing before (see below)

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you change this to ReactInstanceMap.get(parentComponent)? Only that file should have _reactInternalInstance hardcoded.

parentComponent._reactInternalInstance._context
) : emptyObject;

var prevComponent = getTopLevelWrapperInContainer(container);

Expand All @@ -453,6 +458,7 @@ var ReactMount = {
ReactMount._updateRootComponent(
prevComponent,
nextWrappedElement,
nextContext,
container,
updatedCallback
);
Expand Down Expand Up @@ -501,11 +507,7 @@ var ReactMount = {
nextWrappedElement,
container,
shouldReuseMarkup,
parentComponent != null ?
parentComponent._reactInternalInstance._processChildContext(
parentComponent._reactInternalInstance._context
) :
emptyObject
nextContext
)._renderedComponent.getPublicInstance();
if (callback) {
callback.call(component);
Expand Down
2 changes: 1 addition & 1 deletion src/renderers/native/ReactNativeMount.js
Expand Up @@ -118,7 +118,7 @@ var ReactNativeMount = {
var prevWrappedElement = prevComponent._currentElement;
var prevElement = prevWrappedElement.props;
if (shouldUpdateReactComponent(prevElement, nextElement)) {
ReactUpdateQueue.enqueueElementInternal(prevComponent, nextWrappedElement);
ReactUpdateQueue.enqueueElementInternal(prevComponent, nextWrappedElement, emptyObject);
if (callback) {
ReactUpdateQueue.enqueueCallbackInternal(prevComponent, callback);
}
Expand Down
5 changes: 3 additions & 2 deletions src/renderers/shared/stack/reconciler/ReactUpdateQueue.js
Expand Up @@ -246,8 +246,9 @@ var ReactUpdateQueue = {
enqueueUpdate(internalInstance);
},

enqueueElementInternal: function(internalInstance, newElement) {
internalInstance._pendingElement = newElement;
enqueueElementInternal: function(internalInstance, nextElement, nextContext) {
internalInstance._pendingElement = nextElement;
internalInstance._context = nextContext;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Technically we should probably queue context here too (_pendingContext or something) but it looks like the difference is probably unobservable right now? So I guess this is fine. Can you add a TODO here though?

enqueueUpdate(internalInstance);
},

Expand Down