我需要将React中的导航组件包装在充当链接/a的div中(我已经在导航项中嵌套了一个<a></a>
,并且可以在其中嵌套另一个<a></a>
。我需要重新路由用户单击充当包装器的最外层 div 即可到达新路线。我有多个导航部分,想要使用类似 onClick={this.handleNavClick(newRoute)}
的内容,但没有取得任何成功。我正在控制台记录正确的 linkPath,但没有记录任何内容单击时发生。
这是我的功能:
handleNavClick(linkPath) {
console.log(linkPath)
let currentPath = window.location.pathname;
currentPath = window.location.linkPath;
},
这是我尝试从导航部分重新路由的示例:
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>
);
},
最佳答案
你应该尝试这样的事情:
onClick={() => {this.handleNavClick(dashboardsPath)}}
关于javascript - 更改div react 的onClick路线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45361554/