react props
this.props.children/React.Children
this.props.children 表示组件的所有子节点
this.props.children 与 React.Children 区别
1.函数作为child传递
this.props.children.map is not a function
React.Children.map 不会报错
2.count=>获取子组件个数
this.props.children.length 不可行
React.Children.count(this.props.children) => 返回子组件个数
3. React.Children.toArray
4. 执行单一child
React.children.only(this.props.children)
只能在传递单一child的情况下使用,而且child必须为函数。
5. React.cloneElement
renderChildren() {
return React.Children.map(this.props.children, child => {
return React.cloneElement(child, {
name: this.props.name
})
})
}
属性传递
属性不能跨层传递,也不能反向传递
所以 传递单个的props多次,很麻烦也不容易改动
{...this.props} => 获取父组件的props 传给 子组件