user3813862
user3813862

Reputation: 97

How to interpolate anchor tag in a text in React?

I have a variable

const linkElement = `Hey this is a link ${<a href="www.google.com">Click me!</a>} that can be clicked}`

This renders as Hey this is a link [Object object] that can be clicked.

How can I render this like, Hey this is a link Click me!(on click goes to google.com) that can be clicked

Upvotes: 3

Views: 1501

Answers (2)

TheDomis4
TheDomis4

Reputation: 162

you would need to wrap your text inside a react component. A React component cannot be part of a string as it has no concept of a react component.

example:

LinkElement = () => <span>
  Hey this is a link <a href="www.google.com">Click me!</a> that can be clicked
<span>

//and the place you want to use it
<LinkElement />

Upvotes: 2

Aitreya Verma
Aitreya Verma

Reputation: 58

You can do something like this

const linkElement = <>Hey this is a link<a href="www.google.com">Click me !</a> that can be clicked</>

Upvotes: 2

Related Questions