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 中的高阶组件主要是对于 hooks 之前的类组件来说的,如...
我们上一节了解了组件的更新机制,但是只是停留在表层上,例...
我们上一节了解了 react 的虚拟 dom 的格式,如何把虚拟 dom...
react 本身提供了克隆组件的方法,但是平时开发中可能很少使...
mobx 是一个简单可扩展的状态管理库,中文官网链接。小编在接...
我们在平常的开发中不可避免的会有很多列表渲染逻辑,在 pc ...