From e1ec9a6c65cbaf5c0dbdb976a0c86e86c4485b0c Mon Sep 17 00:00:00 2001 From: Ben Alpert Date: Sun, 29 Dec 2013 12:19:10 -0700 Subject: [PATCH] Don't return null children from flattenChildren This simplifies ReactMultiChild a little bit and will make it more practical to coalesce adjacent text strings. --- src/core/ReactMultiChild.js | 22 +++++++++------------- src/utils/flattenChildren.js | 7 +++++-- src/utils/sliceChildren.js | 15 ++++++--------- 3 files changed, 20 insertions(+), 24 deletions(-) diff --git a/src/core/ReactMultiChild.js b/src/core/ReactMultiChild.js index 9fe658c4f6c..dd9ce4e2e0b 100644 --- a/src/core/ReactMultiChild.js +++ b/src/core/ReactMultiChild.js @@ -191,7 +191,7 @@ var ReactMultiChild = { this._renderedChildren = children; for (var name in children) { var child = children[name]; - if (children.hasOwnProperty(name) && child) { + if (children.hasOwnProperty(name)) { // Inlined for performance, see `ReactInstanceHandles.createReactID`. var rootID = this._rootNodeID + '.' + name; var mountImage = child.mountComponent( @@ -220,8 +220,7 @@ var ReactMultiChild = { var prevChildren = this._renderedChildren; // Remove any rendered children. for (var name in prevChildren) { - if (prevChildren.hasOwnProperty(name) && - prevChildren[name]) { + if (prevChildren.hasOwnProperty(name)) { this._unmountChildByName(prevChildren[name], name); } } @@ -293,20 +292,15 @@ var ReactMultiChild = { lastIndex = Math.max(prevChild._mountIndex, lastIndex); this._unmountChildByName(prevChild, name); } - if (nextChild) { - this._mountChildByNameAtIndex( - nextChild, name, nextIndex, transaction - ); - } - } - if (nextChild) { - nextIndex++; + this._mountChildByNameAtIndex( + nextChild, name, nextIndex, transaction + ); } + nextIndex++; } // Remove children that are no longer present. for (name in prevChildren) { if (prevChildren.hasOwnProperty(name) && - prevChildren[name] && !(nextChildren && nextChildren[name])) { this._unmountChildByName(prevChildren[name], name); } @@ -323,7 +317,8 @@ var ReactMultiChild = { var renderedChildren = this._renderedChildren; for (var name in renderedChildren) { var renderedChild = renderedChildren[name]; - if (renderedChild && renderedChild.unmountComponent) { + // TODO: When is this not true? + if (renderedChild.unmountComponent) { renderedChild.unmountComponent(); } } @@ -413,6 +408,7 @@ var ReactMultiChild = { * @private */ _unmountChildByName: function(child, name) { + // TODO: When is this not true? if (ReactComponent.isValidComponent(child)) { this.removeChild(child); child._mountImage = null; diff --git a/src/utils/flattenChildren.js b/src/utils/flattenChildren.js index 17b24ef2208..86e50589474 100644 --- a/src/utils/flattenChildren.js +++ b/src/utils/flattenChildren.js @@ -35,11 +35,14 @@ function flattenSingleChildIntoContext(traverseContext, child, name) { 'Children keys must be unique.', name ); - result[name] = child; + if (child != null) { + result[name] = child; + } } /** - * Flattens children that are typically specified as `props.children`. + * Flattens children that are typically specified as `props.children`. Any null + * children will not be included in the resulting object. * @return {!object} flattened children keyed by name. */ function flattenChildren(children) { diff --git a/src/utils/sliceChildren.js b/src/utils/sliceChildren.js index 7cd01c1fce0..f9283b98ba6 100644 --- a/src/utils/sliceChildren.js +++ b/src/utils/sliceChildren.js @@ -42,15 +42,12 @@ function sliceChildren(children, start, end) { continue; } var child = flattenedMap[key]; - // In this version of slice children we ignore empty children. - if (child !== null) { - if (ii >= start) { - slicedChildren[key] = child; - } - ii++; - if (end != null && ii >= end) { - break; - } + if (ii >= start) { + slicedChildren[key] = child; + } + ii++; + if (end != null && ii >= end) { + break; } } return slicedChildren;