[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
[React] Using the classnames library for conditional CSS的更多相关文章
- React 系列教程 1:实现 Animate.css 官网效果
前言 这是 React 系列教程的第一篇,我们将用 React 实现 Animate.css 官网的效果.对于 Animate.css 官网效果是一个非常简单的例子,原代码使用 jQuery 编写,就 ...
- [React] 01 - Intro: javaScript library for building user interfaces
教学视频: http://www.php.cn/code/8217.html React 教程: http://www.runoob.com/react/react-tutorial.html 本篇是 ...
- react - next.js 引用本地图片和css文件
1. 图片 把图片放在/static/文件夹中,在component中用img tag: <img src={'../static/icon.png'} /> 2. css 把css文件放 ...
- react之只用classNames避免字符串拼接
之前在react当中使用了字符串拼接的方式来拼接类名的字符串,这种方法不仅不够方便,还会出现很多问题 使用classNames这个工具,可以省去拼接字符串的烦恼,大大提高开发效率 首先,最简单的使用方 ...
- 转 : CSS Modules详解及React中实践
https://zhuanlan.zhihu.com/p/20495964 CSS 是前端领域中进化最慢的一块.由于 ES2015/2016 的快速普及和 Babel/Webpack 等工具的迅猛发展 ...
- 12 react 基础 的 css 过渡动画 及 动画效果 及 使用 react-transition-group 实现动画
一. 过渡动画 # index.js import React from 'react';import ReactDOM from 'react-dom';import App from './app ...
- React中使用CSS
第一种: 在组件中直接使用style 不需要组件从外部引入css文件,直接在组件中书写. import React, { Component } from "react"; con ...
- react link引入外部css样式的坑
刚开始的代码是这样的,使用react router4.x写的demo路由跳转后,页面的没有渲染,是因为没有引入外部css文件(或者说引入外部文件路径错误) <!DOCTYPE html> ...
- React项目 - 几种CSS实践
前言团队在使用react时,不断探索,使用了很多不同的css实现方式,此篇blog总结了,react项目中常见的几种css解决方案:inline-style/radium/style-componen ...
随机推荐
- Java集合类之栈Stack
package com.test; import java.util.*; public class Demo7_3 { public static void main(String[] args) ...
- 149. Max Points on a Line
题目: Given n points on a 2D plane, find the maximum number of points that lie on the same straight li ...
- RxJava开发精要1-从.NET到RxJava
原文出自<RxJava Essentials> 原文作者 : Ivan Morgillo 译文出自 : 开发技术前线 www.devtf.cn 转载声明: 本译文已授权开发者头条享有独家转 ...
- [译]GotW #6a: Const-Correctness, Part 1
const 和 mutable在C++存在已经很多年了,对于如今的这两个关键字你了解多少? Problem JG Question 1. 什么是“共享变量”? Guru Question 2. con ...
- proc 文件系统调节参数介绍
/proc/net/* snmp文件 Ip: ip项 Forwarding : 是否开启ip_forward,1开启,2关闭 DefaultTTL : IP默认ttl. In ...
- 手势识别官方教程(6)识别拖拽手势用GestureDetector.SimpleOnGestureListener和onTouchEvent
三种现实drag方式 1,在3.0以后可以直接用 View.OnDragListener (在onTouchEvent中调用某个view的startDrag()) 2,onTouchEvent() ...
- open MMT.distributions = null on transaction type: WIP Lot Split
open MMT.distributions = null on transaction type: WIP Lot Split 打开物料事务处理界面,发现事务处理类型为:WIP Lot ...
- JSP个人总结
应用JSP技术开发动态网站 JSP基本语法 默认JSP: <%@ page language="java" contentType="text/html; char ...
- 【转】简单几步让App Store软件下载快如迅雷 -- 不错!!!
原文网址:http://pad.zol.com.cn/237/2376160_all.html 下载速度慢的原因 1)国内用户从苹果软件商店下载软件速度很慢这是大家都知道的事实,究其原因就是苹 ...
- nmon for linux
nmon(为Nigel's performance Monitor的简写) for linux工具是 IBM开源的在POWER, x86, x86_64, Mainframe & now AR ...