Home > OS >  Need to link dynamically to an external URL in React
Need to link dynamically to an external URL in React

Time:03-12

I am trying to create a list of users and have an inline text link to each user's Facebook page. I need this link to be created dynamically from a database where the URL is saved as facebook.com/userpage.

This information is being saved from a form, so I'd prefer the user to not have to enter "http://www" when inputting the data.

Is it possible to use an href to do this? Something like this (which obviously does not work):

<a href="http://www.{user.facebook}">FB profile</a>

CodePudding user response:

Assuming you are using react router,

<Link to={"http://www.facebook.com/"  user.facebook} target="_blank" rel="noopener noreferrer" />

Since React Router version 5.0.1, you can use

CodePudding user response:

You can use a template literal to interpolate the dynamic part of your url

<a href={`http://www.${user.facebook}`}>FB profile</a>
  • Related