• [React] Using the classnames library for conditional CSS


    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

  • 相关阅读:
    字符串的排列
    二叉搜索树与双向链表
    复杂链表的复制
    二叉树中和为某一值的路径
    二叉搜索树的后序遍历序列
    从上往下打印二叉树
    python系统编程(一)
    python网络编程(十三)
    python网络编程(十二)
    python网络编程(十一)
  • 原文地址:https://www.cnblogs.com/Answer1215/p/5642217.html
Copyright © 2020-2023  润新知