简体   繁体   English

React.js — LinkContainer。 阻止链接导航

[英]React.js — LinkContainer. Prevent link from navigating

Basically what I want to do should be simple :) 基本上我想做的应该很简单:)

I have a two links in the menu, Link1 and Link2. 我在菜单中有两个链接,Link1和Link2。

Link1 has a checkboxes. Link1有一个复选框。 If no checkbox is selected I need to prevent navigation to Link2. 如果未选中任何复选框,则需要阻止导航到Link2。

Inside < Nav > I have this code: 在<导航>内部,我有以下代码:

 <LinkContainer activeClassName='active'
                       to={{pathname: '/cleanup/manual/' + this.state.ProjectId, query: {productIds: this.state.ProductIDs.join()}}}><NavItem
          eventKey={3} onClick={ this.onNavItemClick }title='Sop'>Link2</NavItem></LinkContainer>

When hovered with the mouse, the link is structured like this (no checkbox selected): 当鼠标悬停时,链接的结构如下(未选中任何复选框):

http://localhost:3000/#/cleanup/manual/39?productIds=

And when I have selected some checkboxes: 当我选择了一些复选框时:

http://localhost:3000/#/cleanup/manual/39?productIds=62%2C63

I have access to the state where I can see if I have and ProductIDs selected, and this is how I tried to do it: 我可以访问可以查看是否选择了ID和ProductID的状态,这是我尝试执行的操作:

handleSelect(event) {   
if (event === 3 && this.state.ProductIDs.length === 0) {
    NotificationManager.warning('Please select some products');
    this.setState({activeKey: 0});
    return;
}



this.setState({activeKey: event});
},

If statement works just fine, but it always navigates to the Link2 page. if语句可以正常工作,但是它始终导航到Link2页面。 How can I prevent it from navigating there? 如何防止它导航到那里?

您希望在onNavItemClick e.preventDefault()来防止发生默认功能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM