React项目 - 几种CSS实践
前言
团队在使用react时,不断探索,使用了很多不同的css实现方式,此篇blog总结了,react项目中常见的几种css解决方案:inline-style/radium/style-component,只列举了团队项目中用过的一下实现方式,还有其他的不过多展开
css的不足
样式与状态相关的情况越来越多,需要动态、能直接访问组件state的css。
一切样式都是全局,产生的各种命名的痛苦,BEM等命名规则能解决一部分问题,但当你使用三方插件时却无法避免命名冲突。
Vue怎么解决
scoped 属性
动态css的语法 v-bind class style
react中使用css的标准
是否解决了React开发的痛点:局部css,动态css?
是否支持所有css甚至是sass用法?伪类,嵌套,动画,媒体查询?
是否兼容你需要使用的第三方UI库?
是否能和纯css,或者是其他css框架很好共存,以备遇到特殊情况可以有方案B?
性能?大小?
react 原生css
inline style
const textStyles = {
color: 'white',
backgroundColor: this.state.bgColor
};
<p style={textStyles}>inline style</p>
缺点:
发明了一套新的 css-in-js 语法,使用驼峰化名称等一些规则
不支持所有的 css,例如 media queries, browser states (:hover, :focus, :active) and modifiers (no more .btn-primary!).
inline 写法如果直接同行写影响代码阅读,不清晰优雅
Radium
Features:
Conceptually simple extension of normal inline styles 原生css扩展,改良版
Browser state styles to support :hover, :focus, and :active 支持浏览器样式
Media queries 支持媒体查询
Automatic vendor prefixing 自动组件前缀 scope
Keyframes animation helper 写动画更方便,封装Keyframes
ES6 class and createClass support 支持react类和createClass的写法
简单使用:
<Button kind="primary">Radium Button</Button>
import Radium from 'radium';
import React from 'react';
import color from 'color';
class Button extends React.Component {
static propTypes = {
kind: PropTypes.oneOf(['primary', 'warning']).isRequired
};
render() {
return (
<button
// 多个样式使用数组方便合并
style={[
styles.base,
styles[this.props.kind]
]}>
{this.props.children}
</button>
);
}
}
// 使用了HOC的方式注入样式
// Radium's primary job is to apply interactive or media query styles, but even // if you are not using any special styles, the higher order component will still:
// Merge arrays of styles passed as the style attribute
// Automatically vendor prefix the style object
// Radium(config)(component) 还可以传入一些配置
Button = Radium(Button);
var styles = {
base: {
color: '#fff',
':hover': {
background: color('#0074d9').lighten(0.2).hexString()
}
},
primary: {
background: '#0074D9'
},
warning: {
background: '#FF4136'
}
};
keyframes使用
class Spinner extends React.Component {
render () {
return (
<div>
<div style={styles.inner} />
</div>
);
}
}
Spinner = Radium(Spinner);
var pulseKeyframes = Radium.keyframes({
'0%': {width: '10%'},
'50%': {width: '50%'},
'100%': {width: '10%'},
}, 'pulse');
var styles = {
inner: {
// Use a placeholder animation name in `animation`
animation: 'x 3s ease 0s infinite',
// Assign the result of `keyframes` to `animationName`
animationName: pulseKeyframes,
background: 'blue',
height: '4px',
margin: '0 auto',
}
};
Css Modules
适用于所有使用 webpack 等打包工具的开发环境
{
loader: "css-loader",
options: {
importLoaders: 1,
modules: true,
localIdentName: "[name]__[local]___[hash:base64:5]" // 为了生成类名不是纯随机
},
},
import styles from './table.css';
render () {
return <div className={styles.table}>
<div className={styles.row}>
<div className={styles.cell}>A0</div>
<div className={styles.cell}>B0</div>
</div>
</div>;
}
/* table.css */
.table {}
.row {}
.cell {}
缺点:
class名必须是驼峰形式,否则不能正常在js里使用 styles.table 来引用
由于css模块化是默认,当你希望使用正常的全局css时,需要通过:local 和 :global 切换,不方便
所有的 className 都必须使用 {style.className} 的形式
写在外部样式文件中,无法处理动态css
优化:
babel-plugin-react-css-modules
可以照常写 'table-size' 之类带横杠的类名
正常书写字符串类名,不用加style前缀
// .bablerc
{
"plugins": [
["react-css-modules", {
// options
}]
]
}
缺点:
不过使用 styleName 遇到三方UI库该怎么办呢
补充:
create-react-app v2 直接使用css-moudues和sass
使用方法为一律将css文件命名为 XXX.modules.css, 以上例,即为 table.modules.css, 即可使用。这一解决法的优雅在于,全局的css可以正常使用,只有带.modules.css后缀的才会被modules化
styled-components
使用 ES6 的模板字符串,在js文件里写纯粹的css。
补充sass常用语法
变量:以$开头/变量需要在字符串之中,在#{}之中
计算: 属性可以使用算式
嵌套: &引用父元素
继承: @extend
重用: @mixin命令,定义一个代码块(可以传参数)/@include命令,调用这个mixin
引入文件: @import
// 变量
$blue : #1875e7;
$side : left;
div {
color : $blue;
}
.rounded {
border-#{$side}-radius: 5px;
}
// 计算
body {
margin: (14px/2);
top: 50px + 100px;
right: $var * 10%;
}
// 嵌套
a {
&:hover { color: #ffb3ff; }
}
// 继承
.class1 {
border: 1px solid #ddd;
}
.class2 {
@extend .class1;
font-size:120%;
}
// 重用
@mixin left($value: 10px) {
float: left;
margin-right: $value;
}
div {
@include left(20px);
}
//引入外部文件
@import "path/filename.scss"
来源:https://segmentfault.com/a/1190000016952542
React项目 - 几种CSS实践的更多相关文章
- React项目中 使用 CSS Module
安装react-app-rewired 由于新的 react-app-rewired@2.x 版本的关系,还需要安装 customize-cra.但是我们这里不需要安装 react-app-rewir ...
- react项目实践——(1)使用webpack创建项目
1. 新建文件夹,命名为项目名称——myapp,并打开myapp文件夹. mkdir webpack-demo && cd webpack-demo 2. 在./myapp中打开命令行 ...
- 创建react项目的几种方法
前言: 构建React项目的几种方式: 构建:create-react-app 快速脚手架 构建:generator-react-webpack 构建:webpack一步一步构建 1)构建:creat ...
- React项目的最佳实践
项目代码 从零开始简书项目 从我第一次接触vue这个框架已经过了快一年的时间,陪伴我从前端小白到前端工程师,前端时间也是使用了 ts+vue这样的组合写代码,明显感觉vue与ts似乎没有产生比较好 ...
- 简述--构建React项目的几种方式
前言: 构建React项目的几种方式: 构建:create-react-app 快速脚手架 构建:generator-react-webpack 构建:webpack一步一步构建 1)构建:creat ...
- 了解CSS in JS(JSS)以及在React项目中配置并使用JSS
目录 认识JSS 什么是JSS JSS 的常见实现 JSS 的好处与坏处 好处 坏处 使用模块化CSS实现JSS 安装插件 在React项目中的tsconfig.json中添加配置 vscode项目中 ...
- create-react-app创建react项目 css模块化处理
用的css预处理器用sass,其他大同小异. 用create-react-app创建项目,执行npm run eject弹出配置文件(此操作不可逆): 配置sass,用的最新的CRA,webpack4 ...
- Umi + Dva + Antd的React项目实践
记录一下最近项目所用到的技术React + Dva + Antd + umi ,以免忘记.之前没有用过它们其中一个,也是慢慢摸索,了解数据整个流程. 先了解下概念 React 不多说,3大框架之一: ...
- 从零开始搭建一个react项目
Nav logo 120 发现 关注 消息 4 搜索 从零开始搭建一个react项目 96 瘦人假噜噜 2017.04.23 23:29* 字数 6330 阅读 32892评论 31喜欢 36 项目地 ...
随机推荐
- 前端之JQuery:JQuery扩展和事件
jQuery之jQuery扩展和事件 一.jQuery事件 常用事件 blur([[data],fn]) 失去焦点 focus([[data],fn]) 获取焦点( 搜索框例子) change([[d ...
- Percona Xtrabackup备份mysql大数据库(完整备份与增量备份)
Percona Xtrabackup备份mysql大数据库(完整备份与增量备份) 文章目录 [隐藏] Xtrabackup简介 Xtrabackup安装 Xtrabackup工具介绍 inno ...
- Python---面向对象---龟鱼游戏
一.定义一个门票系统 门票的原价是100元 当周末的时候门票涨价20% 小孩子半票 计算2个成人和1个小孩的平日票价 ----------------------------------------- ...
- SonarQube规则之坏味道类型
1.Abbreviation As Word In Name (默认 关闭)坏味道 主要检查验证标识符名称中的缩写(连续大写字母)长度,还允许执行骆驼案例命名allowedAbbreviationLe ...
- sass、less中的scoped属性
1.scoped 的实现原理 Vue中的Less 中的 scoped 属性的效果主要是通过 PostCss 实现的.代码示例: //编译前 <template> <div class ...
- D2. Remove the Substring (hard version)
D2. Remove the Substring (hard version) 给字符串s,t,保证t为s的子序列,求s删掉最长多长的子串,满足t仍为s的子序列 记录t中每个字母在s中出现的最右的位置 ...
- 滑动报 Unable to preventDefault inside passive event listener due to target being treated as passive 的解决方法
google浏览器滑动出现以下问题: 解决办法如下:在html元素下添加样式 touch-action: none; html{ touch-action:none; }
- linux gsensor驱动分析【转】
本文转载自:http://blog.sina.com.cn/s/blog_89f592f501013sr2.html 本文以Bma250驱动为例子,详细介绍Gsensor设计的一个模板. gsenso ...
- Linux双网卡绑定和解除
转载双网卡绑定和解除 一定要在服务管理中关闭NetworkManager服务并禁用自动启动,因为NetworkManager服务是实时生效的,一旦设置错,管理员就得回到机房接显示器配置网络连接. 以 ...
- linux中表示系统信息如cpu mem disk等内容都在 /proc
linux中表示系统信息的 内容都在 /proc 要查看系统的任何信息, 如cpu mem 磁盘等等, 都在 /proc下, 如: cpuinfo ,meminfo diskstatus 等等