I'm creating a simple App in React , using React Router v4, with this structure
-Wrapper
-Home /
-Homepage /h
--GalleryContainer /h/gallery
---Gallery /h/gallery
---Item /h/gallery/:itemId
--About /h/about
--Links /h/links
in Links, there is a list of elements that redirects to external links. Problem is, when you click on url www.example.com
, it goes to http://localhost:7770/h/www.example.com
and render 404 link not found
Here is the code
<div className="links">
<ul>
{link.links.map((url,i)=>
<li key={i}><a href={url}>{url}</a></li>)}
</ul>
</div>
with url being www.example.com
or any weblink.
How do you make url goes to www.example.com
and not http://localhost:7770/h/www.example.com
?
You can use the Link component or an HTML anchor tag if you want to redirect to an external link with React Router. Using the < Link > component, you can do it thanks to the pathname and target parameters. It will redirect the user to the specified URL.
URL redirection is forwarding a user from one page to another page. There are basically two types of redirection:- Internal Redirection: Forwarding to internal pages. External Redirection: Forwarding to external pages (Other domains).
You can achieve what you want with a trick using the resolve option of a route. Resolve is some data value that Angular2 will obtain for the route to be initialized. More details you can find here in the official documentation. This will redirect to the external URL.
You should add http://
to the beginning of your url. So your code should be
<li key={i}><a href={"http://"+url}>{url}</a></li>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With