demos源码
每个组件都可以获取到 props.children。它包含组件的开始标签和结束标签之间的内容。
react提供了一系列的方法来方便处理children。
- map
 - forEach
 - count,
 - toArray
 - only
 
实际应用
在实际项目中的应用,我们可以对默认的children进行增加处理来达到我们的目的。很多的UI组件就使用了这个属性。
antd的Button组件:
1  | <Button type="primary">Primary</Button>  | 
最终实现效果:
1  | <button type="button" class="ant-btn ant-btn-primary"><span>Primary</span></button>  | 
模拟实现:
1  | import React, { Component } from "react";  | 
1  | import React, { Component } from 'react';  | 
上边实现过程中使用了React.cloneElement,使用方法如下:
1  | React.cloneElement(  | 
