user2371684
user2371684

Reputation: 1555

Alter text in span tag dynamically with React issue

In my React code I have the following code in a span element.

<span>
 slutdatum: {item.end}
  &nbsp;-&nbsp;{week}&nbsp;veckor
</span>

I would like to change the text in my span element "veckor" if the value of {week} is larger then 1, else the text should be "vecka"

Could this be done with some sort of ternary condition? I am quite new to JavaScript and React.

Upvotes: 1

Views: 302

Answers (1)

Someone Special
Someone Special

Reputation: 13588

You can render it conditionally like follows

<span>
 slutdatum: {item.end}
  &nbsp;-&nbsp;{week}&nbsp;{week > 1 ? 'veckor' : 'vecka' }
</span>

Upvotes: 2

Related Questions