将数据传递给来自Child-1.1的Parent(Parent - > Child-1 - > Child-1.1)

时间:2017-09-30 12:43:05

标签: reactjs

如果我有:父母 - > Child-1 - >儿童1.1(儿童-1是儿童1.1的父母)

要将数据从Child-1.1发送到Parent组件,我需要一个回调函数首先将数据发送到Child-1然后再发送到Parent,或者我可以使用回调函数直接从Child-1.1发送到Parent组件? / p>

从Child-1.1我发送数据到Child-1,因为我做了一个回调函数,onClick在Child-1-1,但是在Child-1我没有任何按钮来做另一个回调函数发送给家长。如果我没有任何按钮可以点击,我该如何发送数据?我用onLoad?这就是为什么我问我是否可以直接从Child-1.1发送到Parent组件,使用来自Child-1.1的onClick触发的回调函数。

谢谢!

1 个答案:

答案 0 :(得分:1)

你必须将函数从Parent组件传递给Child1,然后传递给Child1.1

class Parent extends React.Component {

  action = () => {

  }

  render() {
    return <Child action={this.action}/>;
  }
}

class Child extends React.Component {
  render() {
    return <Child1 action={this.props.action} />;
  }
}

class Child1 extends React.Component {
  render() {
    return <button onClick={this.props.action} />;
  }
}