React: event bubbling through nested components

前端 未结 3 1832
暖寄归人
暖寄归人 2021-02-01 16:13

Let\'s say I have nested components like this:


  
    
      
        
cl
3条回答
  •  萌比男神i
    2021-02-01 16:52

    If you want to distinguish which element was clicked at the end, you may use Event object and find there some interesting stuff, such as DOM element type.

    
     
      this.eventHandler(e)}>Click me
    
    eventHandler = (e) => {
            console.log(e, e.nativeEvent.toElement.nodeName);
    ...}
    

    And you get button or input depending on what you've clicked. That's what i looked for for my project.

    More over you may find a DOM tree in an array representation

    Hope it helps

提交回复
热议问题