我正在使用react-router-dom v4,我想在点击链接导航/路由发生变化后执行回调函数。
<nav>
<ul>
{this.routes.map((route) => <li key={route.id}><Link to={route.path} >{route.title}</Link></li>)}
</ul>
</nav>
<main>
{this.routes.map((route) =>
<Route
key={route.id}
path={route.path}
onEnter={() = > console.log('callback after route change')}
exact
component={route.component} />
)}
</main>
我在 Route 上尝试了 onEnter 但它不起作用。
最佳答案
onEnter 在 react-router v4 中被移除,它在 react-router v3 中可用。
您可以尝试使用您要导航到的组件的 componentWillMount 函数。
希望这对您有所帮助。
关于reactjs - React Router Link 回调函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55041169/