React prevent event bubbling in nested components on click

前端 未结 8 1468
隐瞒了意图╮
隐瞒了意图╮ 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 02:58

    This is not 100% ideal, but if it is either too much of a pain to pass down props in children -> children fashion or create a Context.Provider/Context.Consumer just for this purpose), and you are dealing with another library which has it's own handler it runs before yours, you can also try:

       function myHandler(e) { 
            e.persist(); 
            e.nativeEvent.stopImmediatePropagation();
            e.stopPropagation(); 
       }
    

    From what I understand, the event.persist method prevents an object from immediately being thrown back into React's SyntheticEvent pool. So because of that, the event passed in React actually doesn't exist by the time you reach for it! This happens in grandchildren because of the way React handle's things internally by first checking parent on down for SyntheticEvent handlers (especially if the parent had a callback).

    As long as you are not calling persist on something which would create significant memory to keep creating events such as onMouseMove (and you are not creating some kind of Cookie Clicker game like Grandma's Cookies), it should be perfectly fine!

    Also note: from reading around their GitHub occasionally, we should keep our eyes out for future versions of React as they may eventually resolve some of the pain with this as they seem to be going towards making folding React code in a compiler/transpiler.

    0 讨论(0)
  • 2020-11-27 03:01

    React uses event delegation with a single event listener on document for events that bubble, like 'click' in this example, which means stopping propagation is not possible; the real event has already propagated by the time you interact with it in React. stopPropagation on React's synthetic event is possible because React handles propagation of synthetic events internally.

    stopPropagation: function(e){
        e.stopPropagation();
        e.nativeEvent.stopImmediatePropagation();
    }
    
    0 讨论(0)
  • 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

    0 讨论(0)
  • 2020-11-27 03:05

    This is an easy way to prevent the click event from moving forward to the next component and then call your yourFunction.

    <Button onClick={(e)=> {e.stopPropagation(); yourFunction(someParam)}}>Delete</Button>
    
    0 讨论(0)
  • 2020-11-27 03:12

    The new way to do this is a lot more simple and will save you some time! Just pass the event into the original click handler and call preventDefault();.

    clickHandler(e){
        e.preventDefault();
        //Your functionality here
    }
    
    0 讨论(0)
  • 2020-11-27 03:14

    I had issues getting event.stopPropagation() working. If you do too, try moving it to the top of your click handler function, that was what I needed to do to stop the event from bubbling. Example function:

      toggleFilter(e) {
        e.stopPropagation(); // If moved to the end of the function, will not work
        let target = e.target;
        let i = 10; // Sanity breaker
    
        while(true) {
          if (--i === 0) { return; }
          if (target.classList.contains("filter")) {
            target.classList.toggle("active");
            break;
          }
          target = target.parentNode;
        }
      }
    
    0 讨论(0)
提交回复
热议问题