Skip to content

Commit fc7cf2f

Browse files
committed
Replace mountDepth with isTopLevel
Summary: After #2570, `mountDepth` is only used to enforce that `setProps` and `replaceProps` is only invoked on the top-level component. This replaces `mountDepth` with a simpler `isTopLevel` boolean set by `ReactMount` which reduces the surface area of the internal API and removes the need to thread `mountDepth` throughout React core. Reviewers: @sebmarkbage @zpao Test Plan: Ran unit tests successfully: ``` npm run jest ```
1 parent 70b1426 commit fc7cf2f

12 files changed

Lines changed: 44 additions & 109 deletions

src/browser/server/ReactServerRendering.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ function renderToString(element, context) {
4040

4141
return transaction.perform(function() {
4242
var componentInstance = instantiateReactComponent(element, null);
43-
var markup = componentInstance.mountComponent(id, transaction, 0, context);
43+
var markup = componentInstance.mountComponent(id, transaction, context);
4444
return ReactMarkupChecksum.addChecksumToMarkup(markup);
4545
}, null);
4646
} finally {
@@ -68,7 +68,7 @@ function renderToStaticMarkup(element, context) {
6868

6969
return transaction.perform(function() {
7070
var componentInstance = instantiateReactComponent(element, null);
71-
return componentInstance.mountComponent(id, transaction, 0, context);
71+
return componentInstance.mountComponent(id, transaction, context);
7272
}, null);
7373
} finally {
7474
ReactServerRenderingTransaction.release(transaction);

src/browser/ui/ReactDOMComponent.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -168,15 +168,13 @@ ReactDOMComponent.Mixin = {
168168
* @internal
169169
* @param {string} rootID The root DOM ID for this node.
170170
* @param {ReactReconcileTransaction|ReactServerRenderingTransaction} transaction
171-
* @param {number} mountDepth number of components in the owner hierarchy
172171
* @return {string} The computed markup.
173172
*/
174-
mountComponent: function(rootID, transaction, mountDepth, context) {
173+
mountComponent: function(rootID, transaction, context) {
175174
ReactComponent.Mixin.mountComponent.call(
176175
this,
177176
rootID,
178177
transaction,
179-
mountDepth,
180178
context
181179
);
182180
this._rootNodeID = rootID;

src/browser/ui/ReactDOMTextComponent.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,11 +62,10 @@ assign(ReactDOMTextComponent.prototype, {
6262
*
6363
* @param {string} rootID DOM ID of the root node.
6464
* @param {ReactReconcileTransaction|ReactServerRenderingTransaction} transaction
65-
* @param {number} mountDepth number of components in the owner hierarchy
6665
* @return {string} Markup for this text node.
6766
* @internal
6867
*/
69-
mountComponent: function(rootID, transaction, mountDepth, context) {
68+
mountComponent: function(rootID, transaction, context) {
7069
this._rootNodeID = rootID;
7170
var escapedText = escapeTextForBrowser(this._stringText);
7271

src/browser/ui/ReactMount.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,8 @@ function mountComponentIntoNode(
225225
container,
226226
transaction,
227227
shouldReuseMarkup) {
228-
var markup = this.mountComponent(rootID, transaction, 0, emptyObject);
228+
var markup = this.mountComponent(rootID, transaction, emptyObject);
229+
this._isTopLevel = true;
229230
ReactMount._mountImageIntoNode(markup, container, shouldReuseMarkup);
230231
}
231232

src/browser/ui/__tests__/ReactDOMComponent-test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -328,7 +328,7 @@ describe('ReactDOMComponent', function() {
328328
_owner: null,
329329
_context: null
330330
});
331-
return stubComponent.mountComponent('test', transaction, 0, {});
331+
return stubComponent.mountComponent('test', transaction, {});
332332
};
333333
});
334334

src/browser/ui/dom/__tests__/Danger-test.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ describe('Danger', function() {
3232
it('should render markup', function() {
3333
var markup = instantiateReactComponent(
3434
<div />
35-
).mountComponent('.rX', transaction, 0, {});
35+
).mountComponent('.rX', transaction, {});
3636
var output = Danger.dangerouslyRenderMarkup([markup])[0];
3737

3838
expect(output.nodeName).toBe('DIV');
@@ -44,7 +44,7 @@ describe('Danger', function() {
4444
).mountComponent(
4545
'.rX',
4646
transaction,
47-
0, {}
47+
{}
4848
);
4949
var output = Danger.dangerouslyRenderMarkup([markup])[0];
5050

@@ -55,7 +55,7 @@ describe('Danger', function() {
5555
it('should render wrapped markup', function() {
5656
var markup = instantiateReactComponent(
5757
<th />
58-
).mountComponent('.rX', transaction, 0, {});
58+
).mountComponent('.rX', transaction, {});
5959
var output = Danger.dangerouslyRenderMarkup([markup])[0];
6060

6161
expect(output.nodeName).toBe('TH');

src/core/ReactComponent.js

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,6 @@ var ReactComponent = {
9090
// to track updates.
9191
this._currentElement = element;
9292
this._mountIndex = 0;
93-
this._mountDepth = 0;
9493
},
9594

9695
/**
@@ -103,17 +102,15 @@ var ReactComponent = {
103102
*
104103
* @param {string} rootID DOM ID of the root node.
105104
* @param {ReactReconcileTransaction|ReactServerRenderingTransaction} transaction
106-
* @param {number} mountDepth number of components in the owner hierarchy.
107105
* @return {?string} Rendered markup to be inserted into the DOM.
108106
* @internal
109107
*/
110-
mountComponent: function(rootID, transaction, mountDepth, context) {
108+
mountComponent: function(rootID, transaction, context) {
111109
var ref = this._currentElement.ref;
112110
if (ref != null) {
113111
var owner = this._currentElement._owner;
114112
attachRef(ref, this, owner);
115113
}
116-
this._mountDepth = mountDepth;
117114
// Effectively: return '';
118115
},
119116

src/core/ReactCompositeComponent.js

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,7 @@ var ReactCompositeComponentMixin = assign({},
141141

142142
this._context = null;
143143
this._mountOrder = 0;
144+
this._isTopLevel = false;
144145

145146
// See ReactUpdates.
146147
this._pendingCallbacks = null;
@@ -161,17 +162,15 @@ var ReactCompositeComponentMixin = assign({},
161162
*
162163
* @param {string} rootID DOM ID of the root node.
163164
* @param {ReactReconcileTransaction|ReactServerRenderingTransaction} transaction
164-
* @param {number} mountDepth number of components in the owner hierarchy
165165
* @return {?string} Rendered markup to be inserted into the DOM.
166166
* @final
167167
* @internal
168168
*/
169-
mountComponent: function(rootID, transaction, mountDepth, context) {
169+
mountComponent: function(rootID, transaction, context) {
170170
ReactComponent.Mixin.mountComponent.call(
171171
this,
172172
rootID,
173173
transaction,
174-
mountDepth,
175174
context
176175
);
177176

@@ -233,7 +232,6 @@ var ReactCompositeComponentMixin = assign({},
233232
var markup = this._renderedComponent.mountComponent(
234233
rootID,
235234
transaction,
236-
mountDepth + 1,
237235
this._processChildContext(context)
238236
);
239237
if (inst.componentDidMount) {
@@ -313,7 +311,7 @@ var ReactCompositeComponentMixin = assign({},
313311
*/
314312
replaceProps: function(props, callback) {
315313
invariant(
316-
this._mountDepth === 0,
314+
this._isTopLevel,
317315
'replaceProps(...): You called `setProps` or `replaceProps` on a ' +
318316
'component with a parent. This is an anti-pattern since props will ' +
319317
'get reactively updated when rendered. Instead, change the owner\'s ' +
@@ -781,7 +779,6 @@ var ReactCompositeComponentMixin = assign({},
781779
var nextMarkup = this._renderedComponent.mountComponent(
782780
thisID,
783781
transaction,
784-
this._mountDepth + 1,
785782
context
786783
);
787784
ReactComponentEnvironment.replaceNodeWithMarkupByID(
@@ -890,17 +887,15 @@ var ShallowMixin = assign({},
890887
*
891888
* @param {string} rootID DOM ID of the root node.
892889
* @param {ReactReconcileTransaction|ReactServerRenderingTransaction} transaction
893-
* @param {number} mountDepth number of components in the owner hierarchy
894890
* @return {ReactElement} Shallow rendering of the component.
895891
* @final
896892
* @internal
897893
*/
898-
mountComponent: function(rootID, transaction, mountDepth, context) {
894+
mountComponent: function(rootID, transaction, context) {
899895
ReactComponent.Mixin.mountComponent.call(
900896
this,
901897
rootID,
902898
transaction,
903-
mountDepth,
904899
context
905900
);
906901

src/core/ReactMultiChild.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,6 @@ var ReactMultiChild = {
195195
var mountImage = childInstance.mountComponent(
196196
rootID,
197197
transaction,
198-
this._mountDepth + 1,
199198
context
200199
);
201200
childInstance._mountIndex = index;
@@ -401,7 +400,6 @@ var ReactMultiChild = {
401400
var mountImage = child.mountComponent(
402401
rootID,
403402
transaction,
404-
this._mountDepth + 1,
405403
context
406404
);
407405
child._mountIndex = index;

src/core/__tests__/ReactComponent-test.js

Lines changed: 0 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,13 @@ var ReactInstanceMap;
1616
var ReactTestUtils;
1717

1818
var reactComponentExpect;
19-
var getMountDepth;
2019

2120
describe('ReactComponent', function() {
2221
beforeEach(function() {
2322
React = require('React');
2423
ReactInstanceMap = require('ReactInstanceMap');
2524
ReactTestUtils = require('ReactTestUtils');
2625
reactComponentExpect = require('reactComponentExpect');
27-
28-
getMountDepth = function(instance) {
29-
return ReactInstanceMap.get(instance)._mountDepth;
30-
};
3126
});
3227

3328
it('should throw on invalid render targets', function() {
@@ -231,80 +226,4 @@ describe('ReactComponent', function() {
231226
var instance = ReactTestUtils.renderIntoDocument(element);
232227
expect(instance.isMounted()).toBeTruthy();
233228
});
234-
235-
it('should know its simple mount depth', function() {
236-
var Owner = React.createClass({
237-
render: function() {
238-
return <Child ref="child" />;
239-
}
240-
});
241-
242-
var Child = React.createClass({
243-
render: function() {
244-
return <div />;
245-
}
246-
});
247-
248-
var instance = <Owner />;
249-
instance = ReactTestUtils.renderIntoDocument(instance);
250-
expect(getMountDepth(instance)).toBe(0);
251-
expect(getMountDepth(instance.refs.child)).toBe(1);
252-
});
253-
254-
it('should know its (complicated) mount depth', function() {
255-
var Box = React.createClass({
256-
render: function() {
257-
return <div ref="boxDiv">{this.props.children}</div>;
258-
}
259-
});
260-
261-
var Child = React.createClass({
262-
render: function() {
263-
return <span ref="span">child</span>;
264-
}
265-
});
266-
267-
var Switcher = React.createClass({
268-
getInitialState: function() {
269-
return {tabKey: 'hello'};
270-
},
271-
272-
render: function() {
273-
var child = this.props.children;
274-
275-
return (
276-
<Box ref="box">
277-
<div
278-
ref="switcherDiv"
279-
style={{
280-
display: this.state.tabKey === child.key ? '' : 'none'
281-
}}>
282-
{child}
283-
</div>
284-
</Box>
285-
);
286-
}
287-
});
288-
289-
var App = React.createClass({
290-
render: function() {
291-
return (
292-
<Switcher ref="switcher">
293-
<Child key="hello" ref="child" />
294-
</Switcher>
295-
);
296-
}
297-
});
298-
299-
var root = <App />;
300-
root = ReactTestUtils.renderIntoDocument(root);
301-
302-
expect(getMountDepth(root)).toBe(0);
303-
expect(getMountDepth(root.refs.switcher)).toBe(1);
304-
expect(getMountDepth(root.refs.switcher.refs.box)).toBe(2);
305-
expect(getMountDepth(root.refs.switcher.refs.switcherDiv)).toBe(5);
306-
expect(getMountDepth(root.refs.child)).toBe(7);
307-
expect(getMountDepth(root.refs.switcher.refs.box.refs.boxDiv)).toBe(3);
308-
expect(getMountDepth(root.refs.child.refs.span)).toBe(8);
309-
});
310229
});

0 commit comments

Comments
 (0)