@samitouri / QOS-React-2 / commits / 917e101c2c
Fork 0

The library for web and native user interfaces.

Try harder to find container in ReactMount.findReactContainerForID.

When we render a new component into a container, we now record a reference to the rendered DOM node as `rootElementsByReactRootID[reactRootID]`, so that we can determine the actual container later on, in case `containersByReactRootID[reactRootID]` is no longer the true container.

Ben Newman committed Jul 8, 2013 at 11:41 UTC 917e101c2c687387ebb50d79948094fd2ead2644
1 file changed +38 -5
src/core/ReactMount.js
+38 -5
@@ -25,12 +25,17 @@ var ReactID = require('ReactID');
25
26 var $ = require('$');
27
28 -/** Mapping from reactRoot DOM ID to React component instance. */
28 +/** Mapping from reactRootID to React component instance. */
29 var instanceByReactRootID = {};
30
31 -/** Mapping from reactRoot DOM ID to `container` nodes. */
31 +/** Mapping from reactRootID to `container` nodes. */
32 var containersByReactRootID = {};
33
34 +if (__DEV__) {
35 + /** __DEV__-only mapping from reactRootID to root elements. */
36 + var rootElementsByReactRootID = {};
37 +}
38 +
39 /**
40 * @param {DOMElement} container DOM element that may contain a React component
41 * @return {?*} DOM element that may have the reactRoot ID, or null.
@@ -116,6 +121,13 @@ var ReactMount = {
121 ReactMount.scrollMonitor(container, function() {
122 prevComponent.replaceProps(nextProps, callback);
123 });
124 +
125 + if (__DEV__) {
126 + // Record the root element in case it later gets transplanted.
127 + rootElementsByReactRootID[getReactRootID(container)] =
128 + getReactRootElementInContainer(container);
129 + }
130 +
131 return prevComponent;
132 },
133
@@ -150,6 +162,13 @@ var ReactMount = {
162 container,
163 shouldReuseMarkup
164 );
165 +
166 + if (__DEV__) {
167 + // Record the root element in case it later gets transplanted.
168 + rootElementsByReactRootID[reactRootID] =
169 + getReactRootElementInContainer(container);
170 + }
171 +
172 return nextComponent;
173 },
174
@@ -261,6 +280,9 @@ var ReactMount = {
280 component.unmountComponentFromNode(container);
281 delete instanceByReactRootID[reactRootID];
282 delete containersByReactRootID[reactRootID];
283 + if (__DEV__) {
284 + delete rootElementsByReactRootID[reactRootID];
285 + }
286 return true;
287 },
288
@@ -272,9 +294,20 @@ var ReactMount = {
294 * @return {?DOMElement} DOM element that contains the `id`.
295 */
296 findReactContainerForID: function(id) {
275 - var reatRootID = ReactInstanceHandles.getReactRootIDFromNodeID(id);
276 - // TODO: Consider throwing if `id` is not a valid React element ID.
277 - return containersByReactRootID[reatRootID];
297 + var reactRootID = ReactInstanceHandles.getReactRootIDFromNodeID(id);
298 + var container = containersByReactRootID[reactRootID];
299 +
300 + if (__DEV__) {
301 + var rootElement = rootElementsByReactRootID[reactRootID];
302 + if (rootElement && rootElement.parentNode !== container) {
303 + console.warn(
304 + "ReactMount: Root element has been removed from its original " +
305 + "container. New container:", rootElement.parentNode
306 + );
307 + }
308 + }
309 +
310 + return container;
311 },
312
313 /**