React - Prevent Event Trigger on Parent From Child

前端 未结 3 1260
眼角桃花
眼角桃花 2021-02-01 00:52

I have this scenario, where when parent element is clicked, it flips to show a child element with different colours. Unfortunately, when the user clicks on one of the colours, t

3条回答
  •  抹茶落季
    2021-02-01 01:27

    You can use stopPropagation

    stopPropagation - Prevents further propagation of the current event in the bubbling phase

    var App = React.createClass({
      handleParentClick: function (e) { 
        console.log('parent');
      },
    
      handleChildClick: function (e) {
        e.stopPropagation();
        console.log('child');
      },
    
      render: function() {
        return 

    Click

    ; } }); ReactDOM.render(, document.getElementById('root'));
    
    
    

提交回复
热议问题