luy
Version:
所谓类```React```框架就是**和React用法一模一样**的框架。其实当初制造这个框架的目的是为了能更好的学习React内部结构,了解其原理而制作的玩具。但是随着框架的渐渐成长,代码越来越多,我还是决定将其发展下去. 
66 lines (58 loc) • 2.01 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.Children = undefined;
var _createElement = require('./createElement');
var _utils = require('./utils');
var Children = exports.Children = {
//context不是组件的context而是组件上下文
map: function map(childVnode, callback, context) {
if (childVnode === null || childVnode === undefined) {
return childVnode;
}
if ((0, _utils.typeNumber)(childVnode) !== 7) {
return callback.call(context, childVnode, 0);
}
var ret = [];
(0, _createElement.flattenChildren)(childVnode).forEach(function (oldVnode, index) {
var newVnode = callback.call(context, oldVnode, index);
if (newVnode === null) {
return;
}
ret.push(newVnode);
});
return ret;
},
only: function only(childVnode) {
if ((0, _utils.typeNumber)(childVnode) !== 7) {
return childVnode;
}
throw new Error("React.Children.only expect only one child, which means you cannot use a list inside a component");
},
count: function count(childVnode) {
if (childVnode === null) {
return 0;
}
if ((0, _utils.typeNumber)(childVnode) !== 7) {
return 1;
}
return (0, _createElement.flattenChildren)(childVnode).length;
},
forEach: function forEach(childVnode, callback, context) {
var flatten = (0, _createElement.flattenChildren)(childVnode);
if ((0, _utils.typeNumber)(flatten) === 7) {
(0, _createElement.flattenChildren)(childVnode).forEach(callback, context);
} else {
callback.call(context, childVnode);
}
},
toArray: function toArray(childVnode) {
if (childVnode == null) {
return [];
}
return Children.map(childVnode, function (el) {
return el;
});
}
};