@samitouri / QOS-React-2 / commits / 03578e66b5
Fork 0

The library for web and native user interfaces.

Fix rootElementsByReactRootID bookkeeping in ReactMount.js.

It's not always possible to update `rootElementsByReactRootID` when the contents of the container are re-rendered; for instance, when we call `dangerouslyReplaceNodeWithMarkup` or `dangerouslySetInnerHTML`. Since this bookkeeping is just trying to warn about potentially problematic manipulations of the root element, and we can be relatively sure that a new element with the same ID is logically the same element, this diff avoids warning in such cases.

Ben Newman committed Jul 16, 2013 at 11:42 UTC 03578e66b5d3f0a0a8d90c90f18bd8ab1e3bdc4c
2 files changed +22 -3
src/core/ReactID.js
+1
@@ -160,6 +160,7 @@ function purgeID(id) {
160
161 exports.ATTR_NAME = ATTR_NAME;
162 exports.getID = getID;
163 +exports.rawGetID = internalGetID;
164 exports.setID = setID;
165 exports.getNode = getNode;
166 exports.purgeID = purgeID;
src/core/ReactMount.js
+21 -3
@@ -18,6 +18,7 @@
18
19 "use strict";
20
21 +var invariant = require('invariant');
22 var ReactEventEmitter = require('ReactEventEmitter');
23 var ReactInstanceHandles = require('ReactInstanceHandles');
24 var ReactEventTopLevelCallback = require('ReactEventTopLevelCallback');
@@ -300,10 +301,27 @@ var ReactMount = {
301 if (__DEV__) {
302 var rootElement = rootElementsByReactRootID[reactRootID];
303 if (rootElement && rootElement.parentNode !== container) {
303 - console.warn(
304 - "ReactMount: Root element has been removed from its original " +
305 - "container. New container:", rootElement.parentNode
304 + invariant(
305 + // Call rawGetID here because getID calls isValid which calls
306 + // findReactContainerForID (this function).
307 + ReactID.rawGetID(rootElement) === reactRootID,
308 + 'ReactMount: Root element ID differed from reactRootID.'
309 );
310 +
311 + var containerChild = container.firstChild;
312 + if (containerChild &&
313 + reactRootID === ReactID.rawGetID(containerChild)) {
314 + // If the container has a new child with the same ID as the old
315 + // root element, then rootElementsByReactRootID[reactRootID] is
316 + // just stale and needs to be updated. The case that deserves a
317 + // warning is when the container is empty.
318 + rootElementsByReactRootID[reactRootID] = containerChild;
319 + } else {
320 + console.warn(
321 + 'ReactMount: Root element has been removed from its original ' +
322 + 'container. New container:', rootElement.parentNode
323 + );
324 + }
325 }
326 }
327