React onClick and preventDefault() link refresh/redirect?

前端 未结 13 772
生来不讨喜
生来不讨喜 2020-11-30 06:04

I\'m rendering a link with react:

render: ->
  `upvote`

Then, above I hav

13条回答
  •  春和景丽
    2020-11-30 06:46

    React events are actually Synthetic Events, not Native Events. As it is written here:

    Event delegation: React doesn't actually attach event handlers to the nodes themselves. When React starts up, it starts listening for all events at the top level using a single event listener. When a component is mounted or unmounted, the event handlers are simply added or removed from an internal mapping. When an event occurs, React knows how to dispatch it using this mapping. When there are no event handlers left in the mapping, React's event handlers are simple no-ops.

    Try to use Use Event.stopImmediatePropagation:

    upvote: (e) ->
      e.stopPropagation();
      e.nativeEvent.stopImmediatePropagation();
    

提交回复
热议问题