React prevent event bubbling in nested components on click

前端 未结 8 1529
隐瞒了意图╮
隐瞒了意图╮ 2020-11-27 02:41

Here\'s a basic component. Both the

    and
  • have onClick functions. I want only the onClick on the
  • to fir
8条回答
  •  日久生厌
    2020-11-27 03:03

    You can avoid event bubbling by checking target of event.
    For example if you have input nested to the div element where you have handler for click event, and you don't want to handle it, when input is clicked, you can just pass event.target into your handler and check is handler should be executed based on properties of target.
    For example you can check if (target.localName === "input") { return}.
    So, it's a way to "avoid" handler execution

提交回复
热议问题