码迷,mamicode.com
首页 > Web开发 > 详细

[React] Using the classnames library for conditional CSS

时间:2016-07-05 06:24:08      阅读:184      评论:0      收藏:0      [点我收藏+]

标签:

Classnames is a simple yet versatile javascript utility that joins CSS class names based on a set of conditions. We are going to build a simple toggle switch that relies on state to determine what CSS classes will be applied.

 

//className = require(‘classnames‘)
const className = window.classNames;

class ClassnamesExample extends React.Component {

  constructor(props) {
    super(props);
    this.state = {
      isOn: false
    };
  }

  toggleState = () => { this.setState({isOn: !this.state.isOn}); }

  render() {

    const circleClasses = className({
      circle: true,
      off: !this.state.isOn,
      on: this.state.isOn
    });

    const textClasses = className({
      textOff: !this.state.isOn
    })

    console.log(circleClasses);
  //    <div className="circle off"></div>
  //    <span className="textOff">{this.state.isOn ? ‘ON‘ : ‘OFF‘ }</span>
    return (
      <div onClick={this.toggleState}>

        <div className={circleClasses}></div>
        <span className={textClasses}>{this.state.isOn ? ‘ON‘ : ‘OFF‘ }</span>
      </div>
    );
  }
}

window.onload = () => { ReactDOM.render(<ClassnamesExample/>, document.getElementById(‘app‘)); }

 

 to JsBin

[React] Using the classnames library for conditional CSS

标签:

原文地址:http://www.cnblogs.com/Answer1215/p/5642217.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!