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.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *