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