I have an anchor tags with some attributes but I need to add an attribute based on some attribute.
For example –
<a
className={someName}
onContextMenu={handleClick}
>
I need onContextMenu to be present only if some condition is true. Something like this –
<a
className={someName}
if (true) ? onContextMenu={handleClick} : ''
>
How can I achieve it? Coding in Typescript.
2
Answers
Close. Try
onContextMenu={true ? handleClick : null}
You can either move your boolean logic into the assignment of the property:
or you could potentially move the boolean logic into the event handler itself