gpt4 book ai didi

reactjs - 如何在reactjs中调用stopPropagation?

转载 作者:行者123 更新时间:2023-12-03 07:10:31 25 4
gpt4 key购买 nike

我想阻止点击事件冒泡。因此我在代码中添加了 e.stopPropagation() 。我总是在控制台中遇到错误,提示:Uncaught TypeError: e.stopPropagation is not a function

在reactjs中设置stopPropagation的正确方法是什么?

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

...

},

最佳答案

正确的方法是使用.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

Your event handlers will be passed instances of SyntheticEvent, a cross-browser wrapper around the browser's native event. It has the same interface as the browser's native event, including stopPropagation() and preventDefault(), except the events work identically across all browsers. Event System

关于reactjs - 如何在reactjs中调用stopPropagation?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35914680/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com