Skip to content

Don't return null children from flattenChildren #741

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 7, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 9 additions & 13 deletions src/core/ReactMultiChild.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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);
}
}
Expand Down Expand Up @@ -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);
}
Expand All @@ -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();
}
}
Expand Down Expand Up @@ -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;
Expand Down
7 changes: 5 additions & 2 deletions src/utils/flattenChildren.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
15 changes: 6 additions & 9 deletions src/utils/sliceChildren.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down