Home > Blockchain >  How to interpolate anchor tag in a text in React?
How to interpolate anchor tag in a text in React?

Time:02-03

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

CodePudding user response:

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 />

CodePudding user response:

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</>
  •  Tags:  
  • Related