方法一:ES6模版字符串
className={`title ${index === this.state.active ? 'active' : ''}`}
方法二:join("")
className={["title", index === this.state.active ? "active":null].join(' ')}
方法三:classnames(需要下载classnames)
var classNames = require('classnames');
var Button = React.createClass({
// ...
render () {
var btnClass = classNames({
btn: true,
'btn-pressed': this.state.isPressed,
'btn-over': !this.state.isPressed && this.state.isHovered
});
return <button className={btnClass}>{this.props.label}</button>;
}
});
网友评论