Skip to content

Commit

Permalink
Shares debugID information across modules (#8097)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
goatslacker authored and gaearon committed Oct 27, 2016
1 parent e3688d1 commit 6eebed0
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ var ReactCompositeComponent = require('ReactCompositeComponent');
var ReactEmptyComponent = require('ReactEmptyComponent');
var ReactHostComponent = require('ReactHostComponent');

var getNextDebugID = require('getNextDebugID');
var invariant = require('invariant');
var warning = require('warning');

Expand Down Expand Up @@ -56,8 +57,6 @@ function isInternalComponentType(type) {
);
}

var nextDebugID = 1;

/**
* Given a ReactNode, create an instance that will actually be mounted.
*
Expand Down Expand Up @@ -125,7 +124,7 @@ function instantiateReactComponent(node, shouldHaveDebugID) {
instance._mountImage = null;

if (__DEV__) {
instance._debugID = shouldHaveDebugID ? nextDebugID++ : 0;
instance._debugID = shouldHaveDebugID ? getNextDebugID() : 0;
}

// Internal instances should fully constructed at this point, so they should
Expand Down
21 changes: 21 additions & 0 deletions src/shared/utils/getNextDebugID.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/**
* Copyright 2013-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*
* @providesModule getNextDebugID
* @flow
*/

'use strict';

var nextDebugID = 1;

function getNextDebugID(): number {
return nextDebugID++;
}

module.exports = getNextDebugID;
7 changes: 3 additions & 4 deletions src/test/ReactShallowRenderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,16 @@ var ReactReconciler = require('ReactReconciler');
var ReactUpdates = require('ReactUpdates');

var emptyObject = require('emptyObject');
var getNextDebugID = require('getNextDebugID');
var invariant = require('invariant');

var nextDebugID = 1;

class NoopInternalComponent {
constructor(element) {
this._renderedOutput = element;
this._currentElement = element;

if (__DEV__) {
this._debugID = nextDebugID++;
this._debugID = getNextDebugID();
}
}
mountComponent() {}
Expand All @@ -50,7 +49,7 @@ class NoopInternalComponent {
var ShallowComponentWrapper = function(element) {
// TODO: Consolidate with instantiateReactComponent
if (__DEV__) {
this._debugID = nextDebugID++;
this._debugID = getNextDebugID();
}

this.construct(element);
Expand Down

0 comments on commit 6eebed0

Please sign in to comment.