7

I am new to web development, so this is probably pretty fundamental. I want to add a data attribute to the div tag if the datainfo prop variable is not empty

<div style={{height: props.height - handleHeight()}} data={datainfo ? datainfo : ""}>

But right now the data attribute is added with an empty value if the datavalue is empty. Instead I want it to not appear in the div tag at all.

Can I add the whole thing in the JSX clause or how to do it?

Kasper Hansen
  • 6,307
  • 21
  • 70
  • 106

1 Answers1

3

According to this other answer, you should put false instead of "".

<div style={{height: props.height - handleHeight()}} data={datainfo ? datainfo : false}>
thibpat
  • 714
  • 5
  • 17
  • You should add that `null` and `undefined` are acceptable values as well. Personally, I'd rather use `null` here, rather than `false`. But that's just my preference. – Chris Mar 28 '19 at 12:14
  • 1
    Thanks. data={datainfo} seems to work. But data={datainfo ? datainfo : false} does not. It results in data=false – Kasper Hansen Mar 28 '19 at 12:21