javascript - React Router:在主页组件中单击站点徽标时重新加载主页
问题描述
通常,如果您在主页上单击站点的徽标,它将重新加载页面。但是,鉴于 React 的性质,如果我已经在 Home 组件中,它就不会这样做。我怎样才能解决这个问题?
这是我在 App.js 中的代码:
<Router>
<Switch>
<Route exact path='/' component={Home} />
<Route path="/about" component={About} />
</Switch>
</Router>
解决方案
对于功能组件:
import { useLocation } from 'react-router-dom';
let location = useLocation();
...
if (location.pathname === '/') window.location.reload();
推荐阅读
- android-studio - AndroidX 重构支持会代替 android.support.v4 和 android.support.v7 工作吗?
- r - 在 R 中,如何自动安装使用我自己的包的包?
- oracle - 全局索引的更新是否需要锁定相应条目?
- jquery - Spring MVC 方法不将 JSON 字符串返回到 jQuery ajax 调用 - 第二部分
- c++ - 为什么头文件不能互相包含?
- pandas - 减去索引数据框中的行
- javascript - 如何在组合高图表中隐藏系列数据和类别
- c# - 为什么某些 C# 库函数不遵循“ref”参数传递约定
- android - 如何使用此代码实现下一个和上一个按钮?
- audio - 使用 FFmpeg 将多个音频文件交叉淡化为一个?