如何在导航到不同页面之前执行 onclick? [英] How to execute onclick before navigating to a different page?

查看:15
本文介绍了如何在导航到不同页面之前执行 onclick?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 React 中工作,我有以下场景.

I am working in React and I have the following scenario.

  <a href={link} variant="primary" onClick={this.onClickDoSomething}>
                          here.
  </a>

现在,我希望在单击链接之前执行 onclick.但是我没有看到它发生.

Now, I want the onclick to execute before the link has been clicked. However I do not see it happening.

如何解决这个问题?谢谢.

How to work around this issue ? Thanks.

推荐答案

你的点击处理程序应该是这样的

your click handler should be like this

onClickDoSomething=(link)=>{
//do something,then redirect to given link
window.location.href=link

}

你应该将链接呈现为

<a  variant="primary" onClick={()=>{this.onClickDoSomething(link)}}>
                      here.
</a>

这篇关于如何在导航到不同页面之前执行 onclick?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆