0

I'm trying to understand the code from this codesandbox. I've understood most of it, but I don't get this line:

return <div className={`menu-item ${selected ? "active" : ""}`}>{text}</div>;

selected is a string, and it's never empty, so the empty string truth doesn't apply. So how does this line of code work? Also is this javascript quirk or a react/jsx quirk?

Edit: General response:

It's not empty or null though because selected always assigned a value (the value of the name key). What's even weirder is that if you delete the selected prop from return <MenuItem text={name} key={name} selected = {selected}/>;, the active class will still be added, even though no information should be passed to MenuItem. Also, in MenuItem I've added a console.log for selected and it's not even what's passed in (a string), but rather a boolean appears. Why does that happen?

Disclaimer: I'm not that experienced in JS (evidently) and definitely not in React. What I just don't get is how passing in a not null/not empty string will cause a string to be empty? Perhaps I'm missing the part of the code where this.state.selected will lose its value while being passed?

Edit 2:

Y'all were all right. The component did the processing, but this processing wasn't documented, leading to confusion.

Jack Bashford
  • 43,180
  • 11
  • 50
  • 79
user2793618
  • 305
  • 4
  • 10
  • there are concepts called truthy (translates to true in bool comparison), falsy( translates to false in bool comparison) in javascripts. If the string is empty or null, it will evaluate to false meaning null or empty is a falsy – Dan Hunex Jun 19 '19 at 00:15
  • It's not empty or null though because selected always assigned a value (the value of the name key). What's even weirder is that if you delete the selected prop from ```return ;```, the active class will still be added, even though no information should be passed to MenuItem. Also, in MenuItem I've added a console.log for selected and it's not even what's passed in (a string), but rather a boolean appears. Why does that happen? – user2793618 Jun 19 '19 at 00:18

4 Answers4

5

if selected is true add class css active, if false does not add a class

Umbro
  • 1,984
  • 12
  • 40
  • 99
  • It's not empty or null though because selected always assigned a value (the value of the name key). What's even weirder is that if you delete the selected prop from ```return ;```, the active class will still be added, even though no information should be passed to MenuItem. Also, in MenuItem I've added a console.log for selected and it's not even what's passed in (a string), but rather a boolean appears. Why does that happen? – user2793618 Jun 19 '19 at 00:21
1

It means that if selected is truthy, then add the class active. If it's not, then don't add a class. So if selected is true, the rendered HTML output will be:

<div class="menu-item active">Text</div>

If it's false or falsy:

<div class="menu-item">Text</div>
Jack Bashford
  • 43,180
  • 11
  • 50
  • 79
  • It's not empty or null though because selected always assigned a value (the value of the name key). What's even weirder is that if you delete the selected prop from ```return ;```, the active class will still be added, even though no information should be passed to MenuItem. Also, in MenuItem I've added a console.log for selected and it's not even what's passed in (a string), but rather a boolean appears. Why does that happen? – user2793618 Jun 19 '19 at 00:23
1

this is a ternary statement that checks to see if selected is true, if that is the case it will be set to active if not it will be set to an empty string

Brad Ball
  • 599
  • 3
  • 5
0

This is simply like this.

let className = "menu-item ";
if (selected) {
  className = className + "active"
}
console.log(className);
Ken Labso
  • 885
  • 9
  • 13