Categories
React Answers

How to use anchors with React Router?

Spread the love

Sometimes, we want to use anchors with React Router.

In this article, we’ll look at how to use anchors with React Router.

How to use anchors with React Router?

To use anchors with React Router, we can use the react-router-hash-link package.

We install it by running

npm install --save react-router-hash-link

Then we use it by writing

import { HashLink as Link } from "react-router-hash-link";

<Link to="home-page#section-three">Section three</Link>;

to use the HashLink component with the to prop set to a URL with the anchor.

Conclusion

To use anchors with React Router, we can use the react-router-hash-link package.

Leave a Reply

Your email address will not be published.

If you like the content of this blog, subscribe to my email list to get exclusive articles not available to anyone else.