React传值问题求助

#1

代码如图,我在程序中使用antd的layout组件来布局,同时this.props中有一个globaluser属性,请问如何在Content中的this.props.children中获取到globaluser呢?

#2

可以使用 React.cloneElement

1 Like
#3

https://zhidao.baidu.com/question/620210340513349852.html

1 Like
#4
const Child = React.createClass({
  render: function() {
    return <div onClick={() => this.props.doSomething(this.props.value)}>Click Me</div>;
  }
});

const Parent = React.createClass({
  doSomething: function(value) {
    console.log('doSomething called by child with value:', value);
  },

  render: function() {
    const childrenWithProps = React.Children.map(this.props.children,
     (child) => React.cloneElement(child, {
       doSomething: this.doSomething
     })
    );

    return <div>{childrenWithProps}</div>
  }
});

ReactDOM.render(
  <Parent>
    <Child value="1" />
    <Child value="2" />
  </Parent>,
  document.getElementById('container')
);

https://jsfiddle.net/wqvmf7or/16/

1 Like
#5

谢谢,已参考解决了