I'd like to do something like Angular does with ng-class. I'm using react-tabs library (https://github.com/mzabriskie/react-tabs) and now I want to be able to change the background of the selected tab. The library comes with a default effect for the selected tab but I want personalize it.
Use Native JavaScript to Set Dynamic className Values in React. The simplest way to add dynamic className values is to combine curly braces and set the className attributes equal to the state variables. This is a fairly simple feature available in all versions of JavaScript.
Class Name Manipulation Edit on GitHubclassSet() is a neat utility for easily manipulating the DOM class string. When using classSet() , pass an object with keys of the CSS class names you might or might not need. Truthy values will result in the key being a part of the resulting string.
if you're using an es6 transpiler you can use template strings and a ternary:
className={`base-class ${this.props.isActive? 'is-active' : ''}`}
This works:
<div className={this.props.canChange ? "change-button" : "change-button gray-out"}>
blah
</div>
Basically classname is a property of the node.
Just wrap your <Tabs>
element with an element of your own <div className="my-tabs">
and go from there! You can use the aria-selected
to style the selected state.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With