首页 > 解决方案 > React Router:在主页组件中单击站点徽标时重新加载主页

问题描述

通常,如果您在主页上单击站点的徽标,它将重新加载页面。但是,鉴于 React 的性质,如果我已经在 Home 组件中,它就不会这样做。我怎样才能解决这个问题?

这是我在 App.js 中的代码:

<Router>
  <Switch>
    <Route exact path='/' component={Home} />
    <Route path="/about" component={About} />
  </Switch>
</Router>

标签: javascriptreactjsreact-router

解决方案


对于功能组件:

import { useLocation } from 'react-router-dom';

let location = useLocation();
...

if (location.pathname === '/') window.location.reload();

推荐阅读