'How to use HashRouter and anchor navigation on the page together in React?

I am using HashRouter from react-router-dom and when I am trying to use an anchor tag:

<a href="#div-id"> Link to div </a>

which links it to a div on the same page with an id div-id, it redirects the user to default Route as the the url .../#/div-id is not mentioned in any of the paths of Route.

How do I achieve directing user to a div with id = "div-id" in the same page I am using anchor tag, while using HashRouter?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source