我需要在React中包裝一個導航組件作爲鏈接/ a(我已經有一個<a></a>
嵌套在導航項目中,並且可以嵌套另一個<a></a>
。將用戶路由到一個新的路線,點擊這個最外面的div,它充當包裝。我有多個導航部分,並且想要使用類似於onClick={this.handleNavClick(newRoute)}
的東西,但沒有取得任何成功。發生在點擊更改路線onClick作出反應
這裏是我的功能:
handleNavClick(linkPath) {
console.log(linkPath)
let currentPath = window.location.pathname;
currentPath = window.location.linkPath;
},
這裏是一個考試我的PLE試圖重新路線從導航部分:
getNavItem() {
const currentPath = window.location.pathname;
const reportPath = "/reporting";
return (
<div onClick={this.handleNavClick(dashboardsPath)}>
<li id="nav-item-view" className={ classNames({"active": currentPath === reportPath}, "sidebar-item")}>
<div className="active-carat"></div>
</li>
</div>
);
},
我建議[反對這雖然](https://stackoverflow.com/questions/36677733/why-jsx-props-should-not-use -arrow-functions) – Ezz
@Ezz是的,你是對的。但Facebook在其文檔中確認了這種方法。 https://facebook.github.io/react/docs/handling-events.html所以沒關係,如果我們沒有任何性能問題。 –