如何调用反应中的停止传播?

我想阻止点击事件冒出来。因此,我在代码中添加了 e.stop 繁殖()。我总是在控制台上出错,上面写着: Uncaught TypeError: e.stopPropagation is not a function

在 reactjs 中设置停止传播的正确方法是什么?

      handleClick: function(index, e) {
e.stopPropagation();


...


},
115556 次浏览

正确的方法是使用 .stopPropagation,

var Component = React.createClass({
handleParentClick: function() {
console.log('handleParentClick');
},


handleChildClick: function(e) {
e.stopPropagation();


console.log('handleChildClick');
},


render: function() {
return <div onClick={this.handleParentClick}>
<p onClick={this.handleChildClick}>Child</p>
</div>;
}
});

Example

事件处理程序将被传递 SyntheticEvent 的实例,即 浏览器的本机事件的跨浏览器包装程序 与浏览器的本机事件相同的界面,包括 静止传播()和阻止默认() ,除非事件正常工作 在所有浏览器上都是一样的。 Event System

export default function myComponent(){


//buttonClicked:
const buttonClicked = (e) => {
e.stopPropagation();
// your code here...
}


//return:
return (
<div>
<input type='button'
onClick={ (e) => buttonClicked(e) }
></input>
</div>
)


}