【问题标题】:How to pass eventhandler to sibling component?如何将事件处理程序传递给兄弟组件?
【发布时间】:2018-10-24 09:39:18
【问题描述】:

我有一个 eventHandler,我希望它的兄弟组件来处理它。 <Brains /> 内部有很多属性(例如 this.randomNumber = 555 内部构造函数),onLeftLeftMove 需要这些属性来处理事件。

render( ) {
  <div>
    <LeftControl onTouchMove={this.onLeftMove} />
    <Brains ..wants to handle onLeftMove because onLeftMove needs access to a lot of properties inside Brains.. />
  </div>
}

如何做到这一点?

【问题讨论】:

标签: javascript reactjs


【解决方案1】:

您需要lift the state up。这意味着共同的祖先 - 呈现 LeftControlBrains 的祖先 - 将保存它们所需的所有信息。

【讨论】:

  • 在这种特殊情况下我无法提升状态。
  • @J.Reku 为什么不呢?你通常可以
猜你喜欢
  • 1970-01-01
  • 2018-01-27
  • 1970-01-01
  • 1970-01-01
  • 2021-06-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多