简体   繁体   English

React.cloneElement:传递新的孩子或复制props.children?

[英]React.cloneElement: pass new children or copy props.children?

I'm confused by the third "children" parameter of React.cloneElement and it's relation to this.props.children . 我对React.cloneElement的第三个“children”参数感到困惑,它与this.props.children的关系。

I followed this guide on higher order components and have the following code: 我在高阶组件上遵循本指南 ,并具有以下代码:

render() {
    const elementsTree = super.render()

    let myPropChange = {}

    /* work on newProps... */
    myPropChange.something = "nice!".

    const newProps = Object.assign({}, elementsTree.props, myPropChange)

    /* map children */
    const newChildren = React.Children.map(elementsTree.props.children, c => something(c))

    return React.cloneElement(elementsTree, newProps, newChildren)
}
  • Should I put the mapped children into my newProps.children or should I pass them as the third parameter to cloneElement ? 我应该将映射的子项放入我的newProps.children还是应该将它们作为第三个参数传递给cloneElement

  • Object.assign copied the children from props to newProps anyway, should I skip them? 无论如何, Object.assign将子项从props复制到newProps ,我应该跳过它们吗?

  • In the guide it says 在指南中说

    Components don't have a guaranty of having the full children tree resolved. 组件无法保证解析完整的子树。

    What does that mean in my situation? 这在我的情况下意味着什么? That this.props.children is not there? 那个this.props.children不在吗?

  • Added 4th question: Why should I clone the props at all and not just directly edit them? 添加了第4个问题:为什么我要克隆道具而不是直接编辑它们?

Should I put the mapped children into my newProps.children or should I pass them as the third parameter to cloneElement ? 我应该将映射的子项放入我的newProps.children还是应该将它们作为第三个参数传递给cloneElement

Either should be fine. 要么应该没事。

Object.assign copied the children from props to newProps anyway, should I skip them? 无论如何, Object.assign将子项从props复制到newProps ,我应该跳过它们吗?

When using cloneElement, you don't need to copy the props yourself. 使用cloneElement时,您不需要自己复制道具。 You can just do React.cloneElement(elementsTree, {something: 'nice!'}) . 你可以做React.cloneElement(elementsTree, {something: 'nice!'})

In the guide it says "Components don't have a guaranty of having the full children tree resolved." 在指南中,它说“组件没有保证解决完整的子树”。 What does that mean in my situation? 这在我的情况下意味着什么? That this.props.children is not there? 那个.props.children不在吗?

I can't be sure what that article meant, but I believe the author means that your component can choose not to use this.props.children . 我无法确定该文章的含义,但我相信作者意味着您的组件可以选择不使用this.props.children For example, if you render <Foo><Bar /></Foo> , then Foo will receive <Bar /> in this.props.children , but if Foo doesn't use this.props.children in its render method, then Bar will never be rendered. 例如,如果渲染<Foo><Bar /></Foo> ,则Foo将在this.props.children接收<Bar /> ,但如果Foo在其render方法中不使用this.props.children ,然后Bar将永远不会被渲染。

Why should I clone the props at all and not just directly edit them? 我为什么要克隆道具而不是直接编辑它们?

React elements are immutable and there's no way you can change the props after an element is created. React元素是不可变的,在创建元素后你无法改变道具。 This allows some performance optimizations in React which wouldn't be possible otherwise. 这允许在React中进行一些性能优化,否则这是不可能的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM