Categories
React Answers

How to add an external link with React Router?

Spread the love

To add an external link with React Router, we can set the to prop to an object with the pathname property set to the external URL we go to when the link is clicked.

For instance, we write

<Link to={{ pathname: "https://example.com" }} target="_blank" />

to set the to prop to { pathname: "https://example.com" } to go to https://example.com when we click on the link.

Also, we set target to _blank so that the link opens a new tab when we click it.

By John Au-Yeung

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

3 replies on “How to add an external link with React Router?”

I found this this to be a better solution , but not for all cases

import React, { useEffect } from “react”;

export default function Contact() {

useEffect(() => {
window.location.href = “https:example.com”;
}, []);

return (

Contact

);
}

Leave a Reply

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