前言
团队在使用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实践的更多相关文章

  1. React项目中 使用 CSS Module

    安装react-app-rewired 由于新的 react-app-rewired@2.x 版本的关系,还需要安装 customize-cra.但是我们这里不需要安装 react-app-rewir ...

  2. react项目实践——(1)使用webpack创建项目

    1. 新建文件夹,命名为项目名称——myapp,并打开myapp文件夹. mkdir webpack-demo && cd webpack-demo 2. 在./myapp中打开命令行 ...

  3. 创建react项目的几种方法

    前言: 构建React项目的几种方式: 构建:create-react-app 快速脚手架 构建:generator-react-webpack 构建:webpack一步一步构建 1)构建:creat ...

  4. React项目的最佳实践

    项目代码 从零开始简书项目 ​ 从我第一次接触vue这个框架已经过了快一年的时间,陪伴我从前端小白到前端工程师,前端时间也是使用了 ts+vue这样的组合写代码,明显感觉vue与ts似乎没有产生比较好 ...

  5. 简述--构建React项目的几种方式

    前言: 构建React项目的几种方式: 构建:create-react-app 快速脚手架 构建:generator-react-webpack 构建:webpack一步一步构建 1)构建:creat ...

  6. 了解CSS in JS(JSS)以及在React项目中配置并使用JSS

    目录 认识JSS 什么是JSS JSS 的常见实现 JSS 的好处与坏处 好处 坏处 使用模块化CSS实现JSS 安装插件 在React项目中的tsconfig.json中添加配置 vscode项目中 ...

  7. create-react-app创建react项目 css模块化处理

    用的css预处理器用sass,其他大同小异. 用create-react-app创建项目,执行npm run eject弹出配置文件(此操作不可逆): 配置sass,用的最新的CRA,webpack4 ...

  8. Umi + Dva + Antd的React项目实践

    记录一下最近项目所用到的技术React + Dva + Antd + umi ,以免忘记.之前没有用过它们其中一个,也是慢慢摸索,了解数据整个流程. 先了解下概念 React 不多说,3大框架之一: ...

  9. 从零开始搭建一个react项目

    Nav logo 120 发现 关注 消息 4 搜索 从零开始搭建一个react项目 96 瘦人假噜噜 2017.04.23 23:29* 字数 6330 阅读 32892评论 31喜欢 36 项目地 ...

随机推荐

  1. 【leetcode】299. Bulls and Cows

    题目如下: 解题思路:本题难度不太大,对时间复杂度也没有很高的要求.我的做法是用一个字典来保存每个字符出现的次数,用正数1记录标记secret中出现的字符,用负数1记录guess中出现的字符,这样每出 ...

  2. 对includes的研究

    1.includes() 方法用来判断一个数组是否包含一个指定的值,如果是返回 true,否则false. 2.let site = ['runoob', 'google', 'taobao']; s ...

  3. LeetCode--105--从前序与中序遍历序列构造二叉树(python)

    根据一棵树的前序遍历与中序遍历构造二叉树. 注意:你可以假设树中没有重复的元素. 例如,给出 前序遍历 preorder = [3,9,20,15,7]中序遍历 inorder = [9,3,15,2 ...

  4. Vue-Awesome-Swiper实现缩略图控制循环,循环背景图,显示多图轮播,点击左右滚动一张图

    效果图: 本姐只展示关键代码哈 上代码:网站有完整代码,但是数据不是循环的.https://surmon-china.github.io/vue-awesome-swiper/ 循环数据的代码在此: ...

  5. [window] Pyhton轻便好用的spyder IDE如何去除E501 line too long提示

    spyder 使用pep8作为代码规范的标准,默认单行长度是89个字符以内. 作为一个完美控,在使用spyer有的进行coding时,每当看到以下这个小小的warning时,心情都不是很爽: 89个字 ...

  6. [USACO07OPEN]Dining 题解

    前言 如果有人不会网络流,那么安利一下我网络最大流Dinic的博客 关于网络流,我多久没有碰这个算法了... 这是一道网络流好题. 题解 这道题目难点主要是构图. 这道题的构图一开始很容易想到建一个超 ...

  7. IDEA 创建spring boot 的Hello World 项目

    1.Open IDEA,choose "New-->Project" 2.Choose "Spring Initializr" 3. Choose jav ...

  8. Codeforces Round #369 (Div. 2) A. Bus to Udayland (水题)

    Bus to Udayland 题目链接: http://codeforces.com/contest/711/problem/A Description ZS the Coder and Chris ...

  9. [CF46D]Parking Lot

    题目:Parking Lot 传送门:http://codeforces.com/problemset/problem/46/D 分析: 做法一: 1)这题和Hotel那题一样,也可以看做是求区间空位 ...

  10. vue中移动端自适应方案

    安装 lib-flexible 1.npm i lib-flexible 2.在项目入口文件 main.js 里 引入 lib-flexible import ‘lib-flexible’ 3.添加m ...