加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-15 20:51:42 所属栏目:百科 来源:网络整理
导读:我正在渲染一个有反应的链接: render: - `a className="upvotes" onClick={this.upvote}upvote/a` 那么上面我有upvote功能: upvote: - // do stuff (ajax) 在链接之前,我已经跨越在那个地方,但我需要切换到链接,这里的麻烦 – 每次我点击.upvotes页面被刷
我正在渲染一个有反应的链接:
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();

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读