【问题标题】:Pass data to Parent from Child-1.1 (Parent -> Child-1 -> Child-1.1)将数据从 Child-1.1 传递给 Parent(Parent -> Child-1 -> Child-1.1)
【发布时间】:2018-03-12 04:26:15
【问题描述】:

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

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

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

谢谢!

【问题讨论】:

  • 你可以看看 redux。通常,当您在组件之间跳跃过多时,它会有所帮助。

标签: reactjs


【解决方案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} />;
  }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-03-22
    • 2011-05-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-05
    • 1970-01-01
    • 2013-11-20
    相关资源
    最近更新 更多