React - Prevent Event Trigger on Parent From Child

前端 未结 3 1267
眼角桃花
眼角桃花 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:14

    Another solution is to attach to the event callback on the parent the following:

    if(event.target == event.currentTarget){
      event.stopPropagation()
      ....
    }
    

    This way you can intercept events, that originated in the attached DOM node and unrelated events are relayed to the next node.

提交回复
热议问题