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

后端 未结 26 3868
猫巷女王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条回答
  •  萌比男神i
    2020-11-22 00:12

    For a slightly cleaner way to do it, try:

    {React.cloneElement(this.props.children, { loggedIn: this.state.loggedIn })}

    Edit: To use with multiple individual children (the child must itself be a component) you can do. Tested in 16.8.6

    {React.cloneElement(props.children[0], { loggedIn: true, testingTwo: true })} {React.cloneElement(props.children[1], { loggedIn: true, testProp: false })}

提交回复
热议问题