I want to conditionally show and hide this button group depending on what is passed in from the parent component which looks like this:
<TopicNav showBulkActions={this.__hasMultipleSelected} />
....
__hasMultipleSelected: function() {
return false; //return true or false depending on data
}
....
var TopicNav = React.createClass({
render: function() {
return (
<div className="row">
<div className="col-lg-6">
<div className="btn-group pull-right {this.props.showBulkActions ? 'show' : 'hidden'}">
<button type="button" className="btn btn-default dropdown-toggle" data-toggle="dropdown" aria-expanded="false">
Bulk Actions <span className="caret"></span>
</button>
<ul className="dropdown-menu" role="menu">
<li><a href="#">Merge into New Session</a></li>
<li><a href="#">Add to Existing Session</a></li>
<li className="divider"></li>
<li><a href="#">Delete</a></li>
</ul>
</div>
</div>
</div>
);
}
});
Nothing is happening however, with the {this.props.showBulkActions ? 'show' : 'hidden'}. Am I doing anything wrong here?
In case you will need only one optional class name:
Expending on @spitfire109's fine answer, one could do something like this:
and then within the render function:
keeps the jsx short
You can use this npm package. It handles everything and has options for static and dynamic classes based on a variable or a function.
Or use npm classnames. It is very easy and useful especially for constructing the list of classes
More elegant solution, which is better for maintenance and readability:
If you are using a transpiler (such as Babel or Traceur) you can use the new ES6 "template strings".
Here is the answer of @spitfire109, modified accordingly:
This approach allows you to do neat things like that, rendering either
s-is-shown
ors-is-hidden
: