[英]React Router v6 onclick
I've asked the same question before but now it's not working with react-router v6.我以前问过同样的问题,但现在它不适用于 react-router v6。
I've simplified the question to how can I output to console when a <Link />
is clicked?我已将问题简化为单击
<Link />
时如何输出到控制台?
Link in side menu component侧边菜单组件中的链接
<Link to={"/entity"}>Entity</Link>
The route from the main app.js来自主 app.js 的路由
<Routes>
<Route path="/entity" element={<Entity />} />
</Routes>
How to perform an action once the link is clicked for a second time (the page is already showing) This example is just outputting to console whenever the <Link>
is clicked, but the output only shows the first time it loads (or mounts)第二次单击链接后如何执行操作(页面已经显示)此示例只是在单击
<Link>
时输出到控制台,但输出仅显示第一次加载(或安装)
function Entity(props) {
console.log('link clicked');
useEffect(() => {
console.log('link clicked');
});
}
The main change from the previous solution to the new one is that RRDv6 doesn't have a Redirect
component and the Link
component API changed slightly, the route state is now a first-level prop instead of being nested on the to
prop object.与之前的解决方案相比,新的解决方案的主要变化是 RRDv6 没有
Redirect
组件并且Link
组件 API 略有变化,路由状态现在是一级 prop 而不是嵌套在to
prop 对象上。
Link关联
<Link
onClick={() => setLinkKey(uuidV4())}
key={linkKey}
to={"/users"}
state={{ key: linkKey }}
>
Users
</Link>
The components rendered on that route still just look for the key to change when clicking that link again if already on the route.如果已经在路由上,则在该路由上呈现的组件仍然只是在再次单击该链接时查找要更改的键。
you can use onClicke like a prop in Link :您可以将 onClicke 用作 Link 中的道具:
<Link to={"/entity"} onClick={()=>{
console.log('link clicked');
useEffect(() => {
console.log('link clicked');
});
}}>Entity</Link>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.