How to execute onclick before navigating to a different page?(如何在导航到不同页面之前执行 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?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!