I am trying to use "or" || operator in my render method of react like this (aim is to decide whether or not to show element)
<div className="item-actions">
{this.state.addedMovieTag.filter(function( obj ) {
return obj.trivia_id === item.id;
}).length > 0 || this.state.addedPersonTag.filter(function( obj ) {
return obj.trivia_id === item.id;
}).length > 0 && <span className="fa fa-edit" onClick={this.saveChanges.bind(this, item)}></span>}
<span className="fa fa-trash"></span>
</div>
now consider following scenarios (aim is to decide whether or not to show )
scenario 1: (span not shown)
this.state.addedMovieTag.filter(function( obj ) {
return obj.trivia_id === item.id;
}).length === 0
this.state.addedPersonTag.filter(function( obj ) {
return obj.trivia_id === item.id;
}).length === 0
scenario 2: (span shows - expected behaviour)
this.state.addedMovieTag.filter(function( obj ) {
return obj.trivia_id === item.id;
}).length === 1
this.state.addedPersonTag.filter(function( obj ) {
return obj.trivia_id === item.id;
}).length === 0
scenario 3: (span shows - expected behaviour)
this.state.addedMovieTag.filter(function( obj ) {
return obj.trivia_id === item.id;
}).length === 2
this.state.addedPersonTag.filter(function( obj ) {
return obj.trivia_id === item.id;
}).length === 0
scenario 4: (span not shows up - UNEXPECTED behaviour)
this.state.addedMovieTag.filter(function( obj ) {
return obj.trivia_id === item.id;
}).length === 2
this.state.addedPersonTag.filter(function( obj ) {
return obj.trivia_id === item.id;
}).length === 1
a || b || c && d. So isdassociated withc? Yes. So ifaorbis true, it will return true. but ifcis false, it will not renderd. Only case when d is rendered is ifaandbis false andcis true. Try(a || b || c) && dinsteada || b && cis different from(a || b) && c(being the latter what you desire)? Also, I would recommend moving that logic out from the view and assign it to a variable so it leaves the view to rendering only with an easy-to-read simple logic e.g.{hasContentToDisplay && <span ...>}.some()function if you're checking forlength > 0after afilter().