[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

时间: 2024-08-02 08:40:18

[React] Using the classnames library for conditional CSS的相关文章

React 系列教程 1:实现 Animate.css 官网效果

前言 这是 React 系列教程的第一篇,我们将用 React 实现 Animate.css 官网的效果.对于 Animate.css 官网效果是一个非常简单的例子,原代码使用 jQuery 编写,就是添加类与删除类的操作.这对于学习 React 来说是一个非常简易的例子,但是我并不会在教程中介绍相关的前置知识,比如 JSX.ES6 等,对于小白来说可能还会有一些困惑的地方,所以还要了解一下 React 相关的基础知识.虽然 React 有很多值得深究的知识,但这个系列教程并不会涉及高大深的内容

React项目 - 几种CSS实践

前言团队在使用react时,不断探索,使用了很多不同的css实现方式,此篇blog总结了,react项目中常见的几种css解决方案:inline-style/radium/style-component,只列举了团队项目中用过的一下实现方式,还有其他的不过多展开 css的不足样式与状态相关的情况越来越多,需要动态.能直接访问组件state的css.一切样式都是全局,产生的各种命名的痛苦,BEM等命名规则能解决一部分问题,但当你使用三方插件时却无法避免命名冲突. Vue怎么解决 scoped 属性

React中使用CSS

第一种: 在组件中直接使用style 不需要组件从外部引入css文件,直接在组件中书写. import React, { Component } from "react"; const div1 = { width: "300px", margin: "30px auto", backgroundColor: "#44014C", //驼峰法 minHeight: "200px", boxSizing: &

CSS Modules如何使用?

本文和大家分享的主要是CSS Modules的相关内容,一起来看看吧,希望对大家学习css有所帮助. 什么是css模块化? 为了理解css模块化思想,我们首先了解下,什么是模块化,在百度百科上的解释是,在系统的结构中,模块是可组合.分解和更换的单元.模块化是一种处理复杂系统分解成为更好的可管理模块的方式.它可以通过在不同组件设定不同的功能,把一个问题分解成多个小的独立.互相作用的组件,来处理复杂.大型的软件.看完模块化,是不是有种拼图的即视感,可以把大图分成各个小图,然后把小图拼成大图,分与合的

[React] Style a React component with styled-components

In this lesson, we remove the mapping between a React component and the styles applied to it via classnames. We write our styles directly within the component, in a real CSS syntax, with the full power of JavaScript. Old: import React, { Component }

React——相关工具概述

Create a New React App Use an integrated toolchain for the best user and developer experience. This page describes a few popular React toolchains which help with tasks like: Scaling to many files and components. Using third-party libraries from npm.

React 2 - 入门

参考: https://www.taniarascia.com/getting-started-with-react/ 1. React is a JavaScript library, 2. React is NOT a framework , 3. React is an open-source project created by Facebook, 4. React is used to build user interfaces (UI) on the front end 创建Reac

React官网学习笔记

欢迎指导与讨论 : ) 前言 本文主要是笔者在React英文官网学习时整理的笔记.由于笔者水平有限,如有错误恳请指出 O(∩_∩)O 一 .Tutoial 篇 1 . React的组件类名的首字母必须是大写  var Comment = React.creatClass({..})  class Comment extends Component(){...} 2 . 我们需要往一个对象里传入一些方法,并把这个对象以参数的形式传到React.creatClass( )里.其中最重要的方法是ren

在react.js上使用antd-design没有样式

两种解决方法: 第一种: 在.babelrc中加入 { "presets": ["es2015", "react"], "plugins":[["import", {"libraryName": "antd", "style": "css"}]] } (PS:注意使用的是双引号,不然会有错误) 第二种: 在webpack.con