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.
Josh Perez committed
Oct 27, 2016 at 02:31 UTC
6eebed0535a5e28effb7200783cede7a4bdab7ec
3 files changed
+26
-7
src/renderers/shared/stack/reconciler/instantiateReactComponent.js
+2
-3
@@ -15,6 +15,7 @@ var ReactCompositeComponent = require('ReactCompositeComponent');
15
var ReactEmptyComponent = require('ReactEmptyComponent');
16
var ReactHostComponent = require('ReactHostComponent');
17
18
+var getNextDebugID = require('getNextDebugID');
19
var invariant = require('invariant');
20
var warning = require('warning');
21
@@ -56,8 +57,6 @@ function isInternalComponentType(type) {
57
);
58
}
59
59
-var nextDebugID = 1;
60
-
60
/**
61
* Given a ReactNode, create an instance that will actually be mounted.
62
*
@@ -125,7 +124,7 @@ function instantiateReactComponent(node, shouldHaveDebugID) {
124
instance._mountImage = null;
125
126
if (__DEV__) {
128
- instance._debugID = shouldHaveDebugID ? nextDebugID++ : 0;
127
+ instance._debugID = shouldHaveDebugID ? getNextDebugID() : 0;
128
}
129
130
// Internal instances should fully constructed at this point, so they should
src/shared/utils/getNextDebugID.js
new
+21
@@ -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
@@ -20,17 +20,16 @@ var ReactReconciler = require('ReactReconciler');
20
var ReactUpdates = require('ReactUpdates');
21
22
var emptyObject = require('emptyObject');
23
+var getNextDebugID = require('getNextDebugID');
24
var invariant = require('invariant');
25
25
-var nextDebugID = 1;
26
-
26
class NoopInternalComponent {
27
constructor(element) {
28
this._renderedOutput = element;
29
this._currentElement = element;
30
31
if (__DEV__) {
33
- this._debugID = nextDebugID++;
32
+ this._debugID = getNextDebugID();
33
}
34
}
35
mountComponent() {}
@@ -50,7 +49,7 @@ class NoopInternalComponent {
49
var ShallowComponentWrapper = function(element) {
50
// TODO: Consolidate with instantiateReactComponent
51
if (__DEV__) {
53
- this._debugID = nextDebugID++;
52
+ this._debugID = getNextDebugID();
53
}
54
55
this.construct(element);