Skip to content

Add reference to parent instance for component instances #1934

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

Closed
wants to merge 1 commit into from
Closed
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
5 changes: 4 additions & 1 deletion src/browser/ReactTextComponent.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ var DOMPropertyOperations = require('DOMPropertyOperations');
var ReactBrowserComponentMixin = require('ReactBrowserComponentMixin');
var ReactComponent = require('ReactComponent');
var ReactDescriptor = require('ReactDescriptor');
var ReactNode = require('ReactNode');

var escapeTextForBrowser = require('escapeTextForBrowser');
var mixInto = require('mixInto');
Expand All @@ -47,6 +48,7 @@ var ReactTextComponent = function(descriptor) {
};

mixInto(ReactTextComponent, ReactComponent.Mixin);
mixInto(ReactTextComponent, ReactNode.Mixin);
mixInto(ReactTextComponent, ReactBrowserComponentMixin);
mixInto(ReactTextComponent, {

Expand All @@ -60,9 +62,10 @@ mixInto(ReactTextComponent, {
* @return {string} Markup for this text node.
* @internal
*/
mountComponent: function(rootID, transaction, mountDepth) {
mountComponent: function(parentNode, rootID, transaction, mountDepth) {
ReactComponent.Mixin.mountComponent.call(
this,
parentNode,
rootID,
transaction,
mountDepth
Expand Down
4 changes: 2 additions & 2 deletions src/browser/server/ReactServerRendering.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ function renderComponentToString(component) {

return transaction.perform(function() {
var componentInstance = instantiateReactComponent(component);
var markup = componentInstance.mountComponent(id, transaction, 0);
var markup = componentInstance.mountComponent(null, id, transaction, 0);
return ReactMarkupChecksum.addChecksumToMarkup(markup);
}, null);
} finally {
Expand All @@ -76,7 +76,7 @@ function renderComponentToStaticMarkup(component) {

return transaction.perform(function() {
var componentInstance = instantiateReactComponent(component);
return componentInstance.mountComponent(id, transaction, 0);
return componentInstance.mountComponent(null, id, transaction, 0);
}, null);
} finally {
ReactServerRenderingTransaction.release(transaction);
Expand Down
7 changes: 5 additions & 2 deletions src/browser/ui/ReactDOMComponent.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ var ReactComponent = require('ReactComponent');
var ReactBrowserEventEmitter = require('ReactBrowserEventEmitter');
var ReactMount = require('ReactMount');
var ReactMultiChild = require('ReactMultiChild');
var ReactNode = require('ReactNode');
var ReactPerf = require('ReactPerf');

var escapeTextForBrowser = require('escapeTextForBrowser');
Expand Down Expand Up @@ -83,7 +84,7 @@ function putListener(id, registrationName, listener, transaction) {

/**
* @constructor ReactDOMComponent
* @extends ReactComponent
* @extends ReactNodeComponent
* @extends ReactMultiChild
*/
function ReactDOMComponent(tag, omitClose) {
Expand All @@ -107,9 +108,10 @@ ReactDOMComponent.Mixin = {
mountComponent: ReactPerf.measure(
'ReactDOMComponent',
'mountComponent',
function(rootID, transaction, mountDepth) {
function(parentNode, rootID, transaction, mountDepth) {
ReactComponent.Mixin.mountComponent.call(
this,
parentNode,
rootID,
transaction,
mountDepth
Expand Down Expand Up @@ -412,6 +414,7 @@ ReactDOMComponent.Mixin = {

mixInto(ReactDOMComponent, ReactComponent.Mixin);
mixInto(ReactDOMComponent, ReactDOMComponent.Mixin);
mixInto(ReactDOMComponent, ReactNode.Mixin);
mixInto(ReactDOMComponent, ReactMultiChild.Mixin);
mixInto(ReactDOMComponent, ReactBrowserComponentMixin);

Expand Down
2 changes: 1 addition & 1 deletion src/browser/ui/__tests__/ReactDOMComponent-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,7 @@ describe('ReactDOMComponent', function() {
_owner: null,
_context: null
});
return stubComponent.mountComponent('test', transaction, 0);
return stubComponent.mountComponent(null, 'test', transaction, 0);
};
});

Expand Down
10 changes: 3 additions & 7 deletions src/browser/ui/dom/__tests__/Danger-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ describe('Danger', function() {
it('should render markup', function() {
var markup = instantiateReactComponent(
<div />
).mountComponent('.rX', transaction, 0);
).mountComponent(null, '.rX', transaction, 0);
var output = Danger.dangerouslyRenderMarkup([markup])[0];

expect(output.nodeName).toBe('DIV');
Expand All @@ -49,11 +49,7 @@ describe('Danger', function() {
it('should render markup with props', function() {
var markup = instantiateReactComponent(
<div className="foo" />
).mountComponent(
'.rX',
transaction,
0
);
).mountComponent(null, '.rX', transaction, 0);
var output = Danger.dangerouslyRenderMarkup([markup])[0];

expect(output.nodeName).toBe('DIV');
Expand All @@ -63,7 +59,7 @@ describe('Danger', function() {
it('should render wrapped markup', function() {
var markup = instantiateReactComponent(
<th />
).mountComponent('.rX', transaction, 0);
).mountComponent(null, '.rX', transaction, 0);
var output = Danger.dangerouslyRenderMarkup([markup])[0];

expect(output.nodeName).toBe('TH');
Expand Down
9 changes: 7 additions & 2 deletions src/core/ReactComponent.js
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ var ReactComponent = {
* @return {?string} Rendered markup to be inserted into the DOM.
* @internal
*/
mountComponent: function(rootID, transaction, mountDepth) {
mountComponent: function(parentNode, rootID, transaction, mountDepth) {
invariant(
!this.isMounted(),
'mountComponent(%s, ...): Can only mount an unmounted component. ' +
Expand All @@ -264,6 +264,7 @@ var ReactComponent = {
var owner = this._descriptor._owner;
ReactOwner.addComponentAsRefTo(this, ref, owner);
}
this._parentNode = parentNode;
this._rootNodeID = rootID;
this._lifeCycleState = ComponentLifeCycle.MOUNTED;
this._mountDepth = mountDepth;
Expand Down Expand Up @@ -409,7 +410,7 @@ var ReactComponent = {
container,
transaction,
shouldReuseMarkup) {
var markup = this.mountComponent(rootID, transaction, 0);
var markup = this.mountComponent(null, rootID, transaction, 0);
mountImageIntoNode(markup, container, shouldReuseMarkup);
},

Expand Down Expand Up @@ -439,6 +440,10 @@ var ReactComponent = {
return null;
}
return owner.refs[ref];
},

getClosestNode: function() {
return this._parentNode;
}
}
};
Expand Down
5 changes: 4 additions & 1 deletion src/core/ReactCompositeComponent.js
Original file line number Diff line number Diff line change
Expand Up @@ -731,9 +731,10 @@ var ReactCompositeComponentMixin = {
mountComponent: ReactPerf.measure(
'ReactCompositeComponent',
'mountComponent',
function(rootID, transaction, mountDepth) {
function(parentNode, rootID, transaction, mountDepth) {
ReactComponent.Mixin.mountComponent.call(
this,
parentNode,
rootID,
transaction,
mountDepth
Expand Down Expand Up @@ -774,6 +775,7 @@ var ReactCompositeComponentMixin = {
// Done with mounting, `setState` will now trigger UI changes.
this._compositeLifeCycleState = null;
var markup = this._renderedComponent.mountComponent(
this.getClosestNode(),
rootID,
transaction,
mountDepth + 1
Expand Down Expand Up @@ -1162,6 +1164,7 @@ var ReactCompositeComponentMixin = {
prevComponentInstance.unmountComponent();
this._renderedComponent = instantiateReactComponent(nextDescriptor);
var nextMarkup = this._renderedComponent.mountComponent(
this.getClosestNode(),
thisID,
transaction,
this._mountDepth + 1
Expand Down
2 changes: 2 additions & 0 deletions src/core/ReactMultiChild.js
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,7 @@ var ReactMultiChild = {
// Inlined for performance, see `ReactInstanceHandles.createReactID`.
var rootID = this._rootNodeID + name;
var mountImage = childInstance.mountComponent(
this.getClosestNode(),
rootID,
transaction,
this._mountDepth + 1
Expand Down Expand Up @@ -397,6 +398,7 @@ var ReactMultiChild = {
// Inlined for performance, see `ReactInstanceHandles.createReactID`.
var rootID = this._rootNodeID + name;
var mountImage = child.mountComponent(
this.getClosestNode(),
rootID,
transaction,
this._mountDepth + 1
Expand Down
33 changes: 33 additions & 0 deletions src/core/ReactNode.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/**
* Copyright 2013-2014 Facebook, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* @providesModule ReactNode
*/

"use strict";

var ReactNode = {

Mixin: {

getClosestNode: function() {
return this;
}

}

};

module.exports = ReactNode;