【发布时间】:2015-12-10 05:10:02
【问题描述】:
假设我有这样的嵌套组件:
<root />
<comp1 />
<comp2 />
<target id={this.props.id}>
<div>click me</div>
我想让点击目标在 root 上运行一个函数:
//on root component
this.action = function(id){}
我是否需要手动设置链中每个组件的属性,就像在 React 教程示例中一样? Jsfiddle
<root />
<comp1 clickHandler={this.action}/>
<comp2 clickHandler={this.clickHandler}/>
<target id={this.props.id} clickHandler={this.clickHandler} />
<div onClick={this.props.clickHandler.bind(this, this.props.id)}>click me</div>
或者有什么方法可以像在普通 DOM 中一样将事件冒泡?
【问题讨论】:
-
事件在 React 中像在 HTML 中一样冒泡:robinwieruch.de/react-event-bubbling-capturing
标签: javascript events reactjs