2016-08-22 97 views
0

基本上我想要做的應該是簡單:)React.js - LinkContainer。防止鏈接導航

我在菜單中有兩個鏈接,Link1和Link2。

Link1有一個複選框。如果沒有選中複選框,我需要阻止導航到Link2。

內部<導航>我有這樣的代碼:

<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> 

當用鼠標懸停,鏈接被構造是這樣的(沒有複選框選擇):

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

當我已經選擇一些複選框:

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

我有權訪問我可以看到的狀態和選擇ProductIDs,這是我如何試圖做到這一點:

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語句工作得很好,但它總是導航到鏈路2頁。我怎樣才能防止它在那裏導航?

+1

您是否嘗試過在'onNavItemClick'方法中使用event的'preventDefault()'? – Igorsvee

+0

我忘了那個。問題解決了。你能否給我一個答案,讓我接受它? 謝謝。 –

回答

1

你想要e.preventDefault()onNavItemClick以防止默認功能發生。