首页 > Web开发 > 详细

[React] Using the classnames library for conditional CSS

时间:2016-07-05 06:21:03      阅读:143      评论: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
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!