反应路由器dom:单击“链接”,刷新当前页面,但不转到目标页面

时间:2020-10-24 13:14:38

标签: reactjs

我在'/ hello'页面上

“ / hello”页面中包含以下组件:

<Link to="/world" onClick={() => window.location.reload()}>
   ...some lines of code
</Link>

当前行为:当我单击“链接”时,“ / hello”页面将刷新,然后什么也没有发生。

预期的行为:单击后,转到“ / world”页面,然后自动刷新/ world页面。

非常感谢您的帮助!

2 个答案:

答案 0 :(得分:0)

我建议您从onClick={() => window.location.reload()}中删除Link。然后,如果您希望每次使用时都重新加载该世界,请在useEffect部分的world组件内部重定向到此页面,定义重新加载。因此,您的世界组件将如下所示:

对于功能组件:

const World = () => {
     useEffect(() => {
        window.location.reload() 
     }, [])

return (
   <>
   </>
)}

export default World

答案 1 :(得分:0)

我不确定这是否是正确的答案, 您为什么不只使用链接重定向到世界页面,并在useEffect中使用刷新

<Link to="/world">
//code
</Link>

在世界页面上

import React,{ useEffect } from "react";



const World = ()=>{
 useEffect(() => {
    window.location.reload;
}, []);
//...
return(
//...)
}