2016-03-10 30 views
11

我想停止点击事件冒泡。因此我在代码中添加了e.stopPropagation()。我总是在控制台中出现错误,它说:Uncaught TypeError: e.stopPropagation is not a function如何在reactjs中调用stopPropagation?

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

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

     ... 

    }, 
+0

你可以发布你的代码吗?你在哪里调用'handleClick'?也许在你的第一个参数'Event'对象 - 尝试调用'console.log(typeof index.stopPropagation)' –

回答

18

正确的方法是使用.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的情况下,围绕着浏览器的原生事件 跨浏览器包装。它有 与浏览器本地事件相同的界面,包括 stopPropagation()和preventDefault(),除了在所有浏览器中以相同的方式运行 。 Event System

相关问题