From 6eebed0535a5e28effb7200783cede7a4bdab7ec Mon Sep 17 00:00:00 2001 From: Josh Perez Date: Thu, 27 Oct 2016 02:31:10 -0700 Subject: [PATCH] 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. --- .../reconciler/instantiateReactComponent.js | 5 ++--- src/shared/utils/getNextDebugID.js | 21 +++++++++++++++++++ src/test/ReactShallowRenderer.js | 7 +++---- 3 files changed, 26 insertions(+), 7 deletions(-) create mode 100644 src/shared/utils/getNextDebugID.js diff --git a/src/renderers/shared/stack/reconciler/instantiateReactComponent.js b/src/renderers/shared/stack/reconciler/instantiateReactComponent.js index 6a8ed0a54ffb1..202768d8bba18 100644 --- a/src/renderers/shared/stack/reconciler/instantiateReactComponent.js +++ b/src/renderers/shared/stack/reconciler/instantiateReactComponent.js @@ -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'); @@ -56,8 +57,6 @@ function isInternalComponentType(type) { ); } -var nextDebugID = 1; - /** * Given a ReactNode, create an instance that will actually be mounted. * @@ -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 diff --git a/src/shared/utils/getNextDebugID.js b/src/shared/utils/getNextDebugID.js new file mode 100644 index 0000000000000..6e7ecb43b84a6 --- /dev/null +++ b/src/shared/utils/getNextDebugID.js @@ -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; diff --git a/src/test/ReactShallowRenderer.js b/src/test/ReactShallowRenderer.js index 1517d49cff824..ee50d9d6efc66 100644 --- a/src/test/ReactShallowRenderer.js +++ b/src/test/ReactShallowRenderer.js @@ -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() {} @@ -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);