[英]How to handle onClick code if user clicks “open link in new tab” in React js
当用户点击我的react项目中的按钮时,我想运行一些跟踪代码。 但是,如果用户在新选项卡中打开它,则不执行单击处理程序。
在React js中是否有任何解决方案。
简单的例子:
var Hello = React.createClass({
render: function() {
function click(event) {
event.preventDefault();
console.log('You can see me even is user clicked on "open link in new tab"')
}
return <div >
< a href = '#'
target = "_blank"
onClick = { click } > Go there < /a> < /div>;
}
});
React.render( < Hello / > , document.body);
这应该工作正常 -
const Hello = React.createClass({
click(event) {
//here this is the component
event.preventDefault();
console.log('You can see me even is user clicked on "open link in new tab"')
},
render() {
return <div >
< a href = '#'
target = "_blank"
onClick = { this.click ///or this.click.bind(this) if you want to have this point to the component } > Go there < /a> < /div>;
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.