2

I have an a-tag looking like this

<a href={ item.htmlReportUrl } target="_blank" rel="noopener noreferrer">
{item.htmlReportText}</a>

which gets it's values for the href and the linktext from this

row.htmlReportUrl = res.db[key].htmlReport
row.htmlReportText = 'HTML Report'

My plan was now to in addtion pass

target="_blank" rel="noopener noreferrer"

in the same way. These attributes needs to be set just sometimes. But doing something like this

<a href={ item.finalUrl } { item.htmlReportAttributes } 

isn't working.

Parsing error: Unexpected token, expected "..."

I guess it's added to the href? How can I pass multiple html attributes at once?

Cedan Misquith
  • 1,134
  • 9
  • 20
Tobi
  • 1,702
  • 2
  • 23
  • 42

3 Answers3

2

Try:

item.htmlReportAttributes["target"] = "_blank";
item.htmlReportAttributes["rel"] = "noopener noreferrer";
<a href={ item.finalUrl } { ...item.htmlReportAttributes } >{item.htmlReportText}</a>
Raman Kishore
  • 542
  • 2
  • 12
0

You should define attribute like this

let customAttr= {'target': '_blank','rel':'noopener noreferrer'}

then pass in html tag as below:

<a href={item.finalUrl} {...customAttr} >Link title</a>
Khushbu
  • 655
  • 7
  • 17
0

If you are checking state for the flag whether you want to use those extra attributes then you could try:

<a href={ item.finalUrl } target={this.state.addExtraAttributes == true ? "_blank" : ""} rel={this.state.addExtraAttributes == true ? "noopener noreferre" : ""} >
Tyler Nichol
  • 635
  • 2
  • 7
  • 28