加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

React.cloneElement

发布时间:2020-12-15 20:38:53 所属栏目:百科 来源:网络整理
导读:作用: 克隆react element, 并传递props,和children React.cloneElement( element,[props],[...children]) // children将替代现有的children, props将和现有的props进行浅合并 ? 使用举例 ??: renderChildren() { return React.Children.map( this .props

作用:

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读