Skip to content

Commit 28e5100

Browse files
goatslackerzpao
authored andcommitted
Shares debugID information across modules (#8097)
Prior to this, React was using a nextDebugID variable that was locally scoped to both `instantiateReactComponent` and `ReactShallowRenderer`. This caused problems when the debugIDs would collide, the `itemMap` in `ReactComponentTreeHook` would be overwritten and tests would fail with the message "Expected onBeforeMountComponent() parent and onSetChildren() to be consistent". This change shares the debugID with both modules thus preventing any collisions in the future. (cherry picked from commit 6eebed0)
1 parent fbe19e9 commit 28e5100

File tree

3 files changed

+26
-7
lines changed

3 files changed

+26
-7
lines changed

src/renderers/shared/stack/reconciler/instantiateReactComponent.js

+2-3
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ var ReactCompositeComponent = require('ReactCompositeComponent');
1515
var ReactEmptyComponent = require('ReactEmptyComponent');
1616
var ReactHostComponent = require('ReactHostComponent');
1717

18+
var getNextDebugID = require('getNextDebugID');
1819
var invariant = require('invariant');
1920
var warning = require('warning');
2021

@@ -56,8 +57,6 @@ function isInternalComponentType(type) {
5657
);
5758
}
5859

59-
var nextDebugID = 1;
60-
6160
/**
6261
* Given a ReactNode, create an instance that will actually be mounted.
6362
*
@@ -125,7 +124,7 @@ function instantiateReactComponent(node, shouldHaveDebugID) {
125124
instance._mountImage = null;
126125

127126
if (__DEV__) {
128-
instance._debugID = shouldHaveDebugID ? nextDebugID++ : 0;
127+
instance._debugID = shouldHaveDebugID ? getNextDebugID() : 0;
129128
}
130129

131130
// Internal instances should fully constructed at this point, so they should

src/shared/utils/getNextDebugID.js

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/**
2+
* Copyright 2013-present, Facebook, Inc.
3+
* All rights reserved.
4+
*
5+
* This source code is licensed under the BSD-style license found in the
6+
* LICENSE file in the root directory of this source tree. An additional grant
7+
* of patent rights can be found in the PATENTS file in the same directory.
8+
*
9+
* @providesModule getNextDebugID
10+
* @flow
11+
*/
12+
13+
'use strict';
14+
15+
var nextDebugID = 1;
16+
17+
function getNextDebugID(): number {
18+
return nextDebugID++;
19+
}
20+
21+
module.exports = getNextDebugID;

src/test/ReactShallowRenderer.js

+3-4
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,16 @@ var ReactReconciler = require('ReactReconciler');
1818
var ReactUpdates = require('ReactUpdates');
1919

2020
var emptyObject = require('emptyObject');
21+
var getNextDebugID = require('getNextDebugID');
2122
var invariant = require('invariant');
2223

23-
var nextDebugID = 1;
24-
2524
class NoopInternalComponent {
2625
constructor(element) {
2726
this._renderedOutput = element;
2827
this._currentElement = element;
2928

3029
if (__DEV__) {
31-
this._debugID = nextDebugID++;
30+
this._debugID = getNextDebugID();
3231
}
3332
}
3433
mountComponent() {}
@@ -48,7 +47,7 @@ class NoopInternalComponent {
4847
var ShallowComponentWrapper = function(element) {
4948
// TODO: Consolidate with instantiateReactComponent
5049
if (__DEV__) {
51-
this._debugID = nextDebugID++;
50+
this._debugID = getNextDebugID();
5251
}
5352

5453
this.construct(element);

0 commit comments

Comments
 (0)