UNPKG

luy

Version:

所谓类```React```框架就是**和React用法一模一样**的框架。其实当初制造这个框架的目的是为了能更好的学习React内部结构,了解其原理而制作的玩具。但是随着框架的渐渐成长,代码越来越多,我还是决定将其发展下去. ![](https://github.com/215566435/Luy/blob/master/luy%20icon2.jpg?raw=true)

57 lines (53 loc) 1.67 kB
import { flattenChildren } from './createElement' import { typeNumber } from './utils' export const Children = { //context不是组件的context而是组件上下文 map(childVnode, callback, context) { if (childVnode === null || childVnode === undefined) { return childVnode } if (typeNumber(childVnode) !== 7) { return callback.call(context, childVnode, 0) } var ret = [] flattenChildren(childVnode).forEach((oldVnode, index) => { let newVnode = callback.call(context, oldVnode, index) if (newVnode === null) { return } ret.push(newVnode) }) return ret }, only(childVnode) { if (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(childVnode) { if (childVnode === null) { return 0 } if (typeNumber(childVnode) !== 7) { return 1 } return flattenChildren(childVnode).length }, forEach(childVnode, callback, context) { let flatten = flattenChildren(childVnode) if (typeNumber(flatten) === 7) { flattenChildren(childVnode).forEach(callback, context); } else { callback.call(context, childVnode) } }, toArray: function (childVnode) { if (childVnode == null) { return []; } return Children.map(childVnode, function (el) { return el; }); } }