Example for Bubbling and Capturing in React.js

后端 未结 1 2005
悲&欢浪女
悲&欢浪女 2020-11-29 04:03

I am looking for an example in handling Bubbling and Capturing in React.js. I found one with JavaScript, but I am having trouble finding the equivalent for React.js.

1条回答
  •  野性不改
    2020-11-29 04:35

    Bubbling and capturing are both supported by React in the same way as described by the DOM spec, except for how you go about attaching handlers.

    Bubbling is as straightforward as with the normal DOM API; simply attach a handler to an eventual parent of an element, and any events triggered on that element will bubble to the parent as long as it's not stopped via stopPropagation along the way:

    Capturing is just as straightforward, though it's mentioned only briefly in the docs. Simply add Capture to the event handler property name:

    In this case, if handleClickViaCapturing calls stopPropagation on the event, the button's onClick handler will not be called.

    This JSBin should demonstrate how capturing, bubbling, and stopPropagation works in React: https://jsbin.com/hilome/edit?js,output

    0 讨论(0)
提交回复
热议问题