How to pass props to {this.props.children}

后端 未结 26 3427
猫巷女王i
猫巷女王i 2020-11-21 23:42

I\'m trying to find the proper way to define some components which could be used in a generic way:


  
  

        
26条回答
  •  被撕碎了的回忆
    2020-11-22 00:13

    Some reason React.children was not working for me. This is what worked for me.

    I wanted to just add a class to the child. similar to changing a prop

     var newChildren = this.props.children.map((child) => {
     const className = "MenuTooltip-item " + child.props.className;
        return React.cloneElement(child, { className });
     });
    
     return 
    {newChildren}
    ;

    The trick here is the React.cloneElement. You can pass any prop in a similar manner

提交回复
热议问题