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.