React.cloneElement

作用:

克隆react element, 并传递props, 和children

React.cloneElement(
  element,
  [props],
  [...children]
)
// children将替代现有的children, props将和现有的props进行浅合并

 

使用举例 🌰:

renderChildren() {
  return React.Children.map(this.props.children, child => {
    return React.cloneElement(child, {
      name: this.props.name
    })
  })
}

注意:props也可以传方法,但是,需要注意绑定this,要不然在新克隆的element中,this就指向新的element了。

posted @ 2018-12-12 20:34  迪迪的博客  阅读(2122)  评论(0编辑  收藏  举报