Skip to content
This repository has been archived by the owner on Dec 16, 2021. It is now read-only.

Make the Children API compatible with React #236

Merged
merged 2 commits into from
Dec 14, 2016
Merged
Changes from 1 commit
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
7 changes: 5 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -170,18 +170,21 @@ const ARR = [];
// This API is completely unnecessary for Preact, so it's basically passthrough.
let Children = {
map(children, fn, ctx) {
if (children == null) return null;
children = Children.toArray(children);
if (ctx && ctx!==children) fn = fn.bind(ctx);
return children.map(fn);
},
forEach(children, fn, ctx) {
if (children == null) return null;
children = Children.toArray(children);
if (ctx && ctx!==children) fn = fn.bind(ctx);
children.forEach(fn);
},
count(children) {
children = Children.toArray(children);
return children.length;
let count = 0;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we skip the iteration here and just do something like this?

return children && children.length || 0;

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, of course! Nice catch

Children.forEach(children, () => count++);
return count;
},
only(children) {
children = Children.toArray(children);
Expand Down