React Router: Reload home page when clicking on the site's logo while at the Home component

Viewed 158

Normally, if you click on a site's logo while you at the home page, it will reload the page. However, given the nature of React, if I am already at the Home component, it won't do that. How can I get around this?

Here's my code in App.js:

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

For a functional component:

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

let location = useLocation();
...

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