[英]ReactJS + React Router: How to disable link from React-Router's <Link>?
使用React-Router的鏈接,我進行了如下設置:
<Link to={...}>{this.props.live? "Live": "Not Live"}</Link>
如果this.props.live
被傳入,我只想顯示一個文本“ Live”,它將直接指向一個新頁面。 如果未傳入this.props.live
顯示文本“ Not Live”,但要從中刪除超鏈接/停用/禁用它。
有可能這樣做嗎? 我嘗試了<div style={{pointerEvents: "none"}}>Not Live</div>
,但是不起作用。
將接受並接受答案。 謝謝!
你應該做這個:
let component;
component=this.props.live? <Link to={...}> Live </Link>: <div> Not Live </div>
render() {
{component}
}
干杯:)
this.props.live
? <Link to={...}> Live </Link>
: <div> Not Live </div>
您可以使用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>
}
}
速記但有爭議的版本
<Link onClick={evt => !this.props.live && evt.preventDefault()} to={...}>
{this.props.live? "Live": "Not Live"}
</Link>
請參閱API文檔
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.