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了。


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM