admin管理员组文章数量:1410717
In my React app, I have links with href
attribute (it is need to open links in new window after right click on link) and on link element is react onClick
method.
See example here:
class SomeComponent extends React.Component {
handleClick(event) {
alert('click. but dont redirect...');
return false;
event.preventDefault();
event.stopPropagation();
}
render() {
return (
<a href="test" onClick={this.handleClick}>Test link</a>
);
}
}
ReactDOM.render(<SomeComponent/>, document.getElementById('container'));
<script src=".1.0/react.min.js"></script>
<script src=".1.0/react-dom.min.js"></script>
<div id="container">
</div>
In my React app, I have links with href
attribute (it is need to open links in new window after right click on link) and on link element is react onClick
method.
See example here:
class SomeComponent extends React.Component {
handleClick(event) {
alert('click. but dont redirect...');
return false;
event.preventDefault();
event.stopPropagation();
}
render() {
return (
<a href="test" onClick={this.handleClick}>Test link</a>
);
}
}
ReactDOM.render(<SomeComponent/>, document.getElementById('container'));
<script src="https://cdnjs.cloudflare./ajax/libs/react/15.1.0/react.min.js"></script>
<script src="https://cdnjs.cloudflare./ajax/libs/react/15.1.0/react-dom.min.js"></script>
<div id="container">
</div>
click on link. onClick
method calls, it is ok, but browser do redirect to page, in href
attribute and I don't know, how can I prevent it.
Is possible have link with href
attribute and onclick
method in React without redirect on to the page in href
attr?
Fiddle.
Share Improve this question edited Apr 28, 2017 at 13:49 Mayank Shukla 105k19 gold badges162 silver badges145 bronze badges asked Apr 28, 2017 at 13:29 tomasnikltomasnikl 1032 gold badges2 silver badges11 bronze badges 4- sorry, correct link: jsfiddle/69z2wepo/77298 – tomasnikl Commented Apr 28, 2017 at 13:29
-
if you don't want to redirect then use this:
<a href='#' onClick={this.handleClick}>Test link</a>
don't give any path inhref
. – Mayank Shukla Commented Apr 28, 2017 at 13:35 - I need href attribute if user wants to open link in new window... – tomasnikl Commented Apr 28, 2017 at 13:53
-
If you do not want to redirect, move
event.preventDefault()
to the top ofhandleClick
. – hobenkr Commented Apr 28, 2017 at 13:59
3 Answers
Reset to default 2You are almost there !!!! You just need to move
event.preventDefault();
event.stopPropagation();
up. And it will work !!!!.
Please find below code . Paste it.
class SomeComponent extends React.Component {
handleClick(event) {
event.preventDefault();
event.stopPropagation();
alert('click. but dont redirect...');
return false;
}
render() {
return (
<a href="test" onClick={this.handleClick}>Test link</a>
);
}
}
ReactDOM.render(<SomeComponent/>, document.getElementById('container'));
in your href try using "javascript:void(0);"
remove preventDefault(); from onClick
本文标签: javascriptReact onClick method od link with href not workingStack Overflow
版权声明:本文标题:javascript - React onClick method od link with href not working - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745065302a2640485.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论