[英]ReactJS + React Router: How to disable link from React-Router's <Link>?
Using React-Router's Link, I have a set up like so: 使用React-Router的链接,我进行了如下设置:
<Link to={...}>{this.props.live? "Live": "Not Live"}</Link>
Where if this.props.live
gets passed in, I would like to simply display a text "Live" which will direct to a new page. 如果
this.props.live
被传入,我只想显示一个文本“ Live”,它将直接指向一个新页面。 If this.props.live
is not passed in, want to display the text "Not Live" but remove the hyperlink from it/deactivate/disable it. 如果未传入
this.props.live
显示文本“ Not Live”,但要从中删除超链接/停用/禁用它。
Is it possible to do so? 有可能这样做吗? I tried
<div style={{pointerEvents: "none"}}>Not Live</div>
, but does not work. 我尝试了
<div style={{pointerEvents: "none"}}>Not Live</div>
,但是不起作用。
Will be upvoting and accepting answer. 将接受并接受答案。 Thank you!
谢谢!
You should do this: 你应该做这个:
let component;
component=this.props.live? <Link to={...}> Live </Link>: <div> Not Live </div>
render() {
{component}
}
Cheers:) 干杯:)
this.props.live
? <Link to={...}> Live </Link>
: <div> Not Live </div>
You can prevent the page to be redirected using evt.preventDefault()
您可以使用
evt.preventDefault()
阻止页面重定向
class MyComponent extends React.Component {
constructor(props) {
this.handleLinkClick = this.handleLinkClick.bind(this);
}
handleLinkClick(evt) {
if (!this.props.live) {
evt.preventDefault(); // will prevent redirection
}
}
render() {
<Link onClick={this.handleLinkClick} to={...}>
{this.props.live? "Live": "Not Live"}
</Link>
}
}
Shorthand but controversial version 速记但有争议的版本
<Link onClick={evt => !this.props.live && evt.preventDefault()} to={...}>
{this.props.live? "Live": "Not Live"}
</Link>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.