reactjs – React onClick和preventDefault()链接刷新/重定向?

前端之家收集整理的这篇文章主要介绍了reactjs – React onClick和preventDefault()链接刷新/重定向?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在渲染一个有反应的链接
render: ->
  `<a className="upvotes" onClick={this.upvote}>upvote</a>`

那么上面我有upvote功能

upvote: ->
  // do stuff (ajax)

链接之前,我已经跨越在那个地方,但我需要切换到链接,这里的麻烦 – 每次我点击.upvotes页面被刷新,我已经尝试到目前为止:

event.preventDefault() – 不工作.

upvote: (e) ->
  e.preventDefault()
  // do stuff (ajax)

event.stopPropagation() – 不工作.

upvote: (e) ->
  e.stopPropagation()
  // do stuff (ajax)

返回假 – 不工作.

upvote: (e) ->
  // do stuff (ajax)
  return false

我还在我的index.html中尝试使用jQuery,但似乎没有任何效果.我该怎么做,我做错了什么?我已经检查了event.type,它的点击,所以我想我应该能够避免重定向不知何故?

对不起,我是菜鸟,当涉及到React.

谢谢!

反应事件实际上是合成事件,而不是本地事件.正如写的 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.

尝试使用Use Event.stopImmediatePropagation:

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

猜你在找的React相关文章