diff --git a/src/renderers/shared/stack/reconciler/ReactChildReconciler.js b/src/renderers/shared/stack/reconciler/ReactChildReconciler.js index 060f5c0ac0..40f589c435 100644 --- a/src/renderers/shared/stack/reconciler/ReactChildReconciler.js +++ b/src/renderers/shared/stack/reconciler/ReactChildReconciler.js @@ -93,8 +93,11 @@ var ReactChildReconciler = { updateChildren: function( prevChildren, nextChildren, + mountImages, removedNodes, transaction, + hostParent, + hostContainerInfo, context) { // We currently don't have a way to track moves here but if we use iterators // instead of for..in we can zip the iterators and check if an item has @@ -127,6 +130,16 @@ var ReactChildReconciler = { // The child must be instantiated before it's mounted. var nextChildInstance = instantiateReactComponent(nextElement, true); nextChildren[name] = nextChildInstance; + // Creating mount image now ensures refs are resolved in right order + // (see https://github.com/facebook/react/pull/7101 for explanation). + var nextChildMountImage = ReactReconciler.mountComponent( + nextChildInstance, + transaction, + hostParent, + hostContainerInfo, + context + ); + mountImages.push(nextChildMountImage); } } // Unmount children that are no longer present. diff --git a/src/renderers/shared/stack/reconciler/ReactMultiChild.js b/src/renderers/shared/stack/reconciler/ReactMultiChild.js index 5f35b7a3cb..474ec774f0 100644 --- a/src/renderers/shared/stack/reconciler/ReactMultiChild.js +++ b/src/renderers/shared/stack/reconciler/ReactMultiChild.js @@ -212,6 +212,7 @@ var ReactMultiChild = { _reconcilerUpdateChildren: function( prevChildren, nextNestedChildrenElements, + mountImages, removedNodes, transaction, context @@ -226,14 +227,28 @@ var ReactMultiChild = { ReactCurrentOwner.current = null; } ReactChildReconciler.updateChildren( - prevChildren, nextChildren, removedNodes, transaction, context + prevChildren, + nextChildren, + mountImages, + removedNodes, + transaction, + this, + this._hostContainerInfo, + context ); return nextChildren; } } nextChildren = flattenChildren(nextNestedChildrenElements); ReactChildReconciler.updateChildren( - prevChildren, nextChildren, removedNodes, transaction, context + prevChildren, + nextChildren, + mountImages, + removedNodes, + transaction, + this, + this._hostContainerInfo, + context ); return nextChildren; }, @@ -339,9 +354,11 @@ var ReactMultiChild = { _updateChildren: function(nextNestedChildrenElements, transaction, context) { var prevChildren = this._renderedChildren; var removedNodes = {}; + var mountImages = []; var nextChildren = this._reconcilerUpdateChildren( prevChildren, nextNestedChildrenElements, + mountImages, removedNodes, transaction, context @@ -353,8 +370,10 @@ var ReactMultiChild = { var name; // `nextIndex` will increment for each child in `nextChildren`, but // `lastIndex` will be the last index visited in `prevChildren`. - var lastIndex = 0; var nextIndex = 0; + var lastIndex = 0; + // `nextMountIndex` will increment for each newly mounted child. + var nextMountIndex = 0; var lastPlacedNode = null; for (name in nextChildren) { if (!nextChildren.hasOwnProperty(name)) { @@ -380,12 +399,14 @@ var ReactMultiChild = { updates, this._mountChildAtIndex( nextChild, + mountImages[nextMountIndex], lastPlacedNode, nextIndex, transaction, context ) ); + nextMountIndex++; } nextIndex++; lastPlacedNode = ReactReconciler.getHostNode(nextChild); @@ -473,17 +494,11 @@ var ReactMultiChild = { */ _mountChildAtIndex: function( child, + mountImage, afterNode, index, transaction, context) { - var mountImage = ReactReconciler.mountComponent( - child, - transaction, - this, - this._hostContainerInfo, - context - ); child._mountIndex = index; return this.createChild(child, afterNode, mountImage); }, diff --git a/src/renderers/shared/stack/reconciler/__tests__/ReactMultiChildReconcile-test.js b/src/renderers/shared/stack/reconciler/__tests__/ReactMultiChildReconcile-test.js index b0f190b6cf..50a1a01e20 100644 --- a/src/renderers/shared/stack/reconciler/__tests__/ReactMultiChildReconcile-test.js +++ b/src/renderers/shared/stack/reconciler/__tests__/ReactMultiChildReconcile-test.js @@ -60,6 +60,14 @@ var StatusDisplay = React.createClass({ return this.state.internalState; }, + componentDidMount: function() { + this.props.onFlush(); + }, + + componentDidUpdate: function() { + this.props.onFlush(); + }, + render: function() { return (