Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Reactjs React router dom:单击链接,刷新当前页面,但不';不要转到目标页面_Reactjs - Fatal编程技术网

Reactjs React router dom:单击链接,刷新当前页面,但不';不要转到目标页面

Reactjs React router dom:单击链接,刷新当前页面,但不';不要转到目标页面,reactjs,Reactjs,我在“/你好”页 我在“/hello”页面中有以下组件: <Link to="/world" onClick={() => window.location.reload()}> ...some lines of code </Link> window.location.reload()}> …一些代码行 当前行为:当我点击链接时,“/hello”页面将刷新,然后什么也不会发生 预期行为:单击后,转到“/world”页面,然后自动刷新/w

我在“/你好”页

我在“/hello”页面中有以下组件:

<Link to="/world" onClick={() => window.location.reload()}>
   ...some lines of code
</Link>
window.location.reload()}>
…一些代码行
当前行为:当我点击链接时,“/hello”页面将刷新,然后什么也不会发生

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


非常感谢你的帮助

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

对于功能组件:

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

return (
   <>
   </>
)}

export default World
constworld=()=>{
useffect(()=>{
window.location.reload()
}, [])
返回(
)}
导出默认世界

我不确定这是否是正确答案, 为什么不直接使用link重定向到世界页面,并在useffect中使用refresh呢

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

嗨,谢谢你的回答!是否要使用基于类的组件?很抱歉,我是新手,因此我不熟悉
componentDidMount
部分中的UseEffect定义重新加载。谢谢,但是添加componentDidMount将使其继续重新加载。有办法解决吗?哦,我明白了。另一种方法是使用
window.location.href
作为
onClick
函数并为其分配地址。这将导致在导航到新页面时刷新。
import React,{ useEffect } from "react";



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