经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » JS/JS库/框架 » React » 查看文章
React中使用外部样式的3种方式(小结)
来源:jb51  时间:2019/5/28 10:28:08  对本文有异议

一、关于css-in-js的认识

1、css-in-js是一种使用 js 编写 css 样式的 css 处理方案。它的实现方案有很多,比如styled-componentspolishedglamorous(paypal 开源的,不再维护)、radium、emotion等等。

2、其中最成熟的便是styled-components和emotion。它们真正意义上实现了组件化style,可以说是专门为 react 打造的。

二、styled-components 简介

styled-components是 css-in-js 主流的实现方案,同时也是组件化style的主流实现方案。

下面是styled-components的一些特性:

1、唯一class类名:和 css-module 异曲同工,生成唯一类名,避免重复和全局污染,也不需要你费脑筋思考该如何命名。

2、无冗余css代码:它的样式和组件绑定,组件调用则样式起作用。意味着你不需要关心如何检测和删除那些未使用的 css 代码。

3、动态样式: 它可以很简单地调整和拓展组件的样式,而不需要建立很多个 class 类来维护组件的样式。

4、自动添加兼容前缀:和 Autoprefixer 类似,会自动添加浏览器兼容前缀以支持旧版浏览器。

5、支持变量和继承:你可以使用变量来设置不同的样式,使用这些不同样式时只需要给样式组件传递一个参数即可。

三、styled-components使用方式

1、安装

  1. npm install styled-components

2、使用

styled-components主要基于 es6 的标签模板语法调用标签函数

  1. import React, { Component } from 'react'
  2. import styled from 'styled-components'
  3.  
  4. export default class Style extends Component {
  5. render() {
  6. return (
  7. <>
  8. <div>
  9. <Title>我是标题</Title>
  10. </div>
  11. </>
  12. )
  13. }
  14. }
  15.  
  16. // 使用es6的模板字符串的方式(下面表示定义了h1的样式)
  17. const Title = styled.h1`
  18. font-size: 20px;
  19. color: #f00;

3、嵌套的使用

  1. import React, { Component } from 'react'
  2. import styled from 'styled-components'
  3.  
  4. export default class Style extends Component {
  5. render() {
  6. return (
  7. <>
  8. <div>
  9. <Content>
  10. <h2>你好</h2>
  11. <div className="content">我是内容</div>
  12. </Content>
  13. </div>
  14. </>
  15. )
  16. }
  17. }
  18.  
  19. const Content = styled.div`
  20. width: 100%;
  21. height: 500px;
  22. border: 1px solid #f00;
  23. > h2 {
  24. color: pink;
  25. }
  26. > .content {
  27. text-align: center;
  28. color: #f00;
  29. }
  30. `
  31.  

4、使用props传递参数的方式

  1. import React, { Component } from 'react'
  2. import styled, { css } from 'styled-components'
  3.  
  4. export default class Style2 extends Component {
  5. render() {
  6. return (
  7. <div>
  8. <Button> 提交 </Button>
  9. <Button primary> 提交 </Button>
  10. </div>
  11. )
  12. }
  13. }
  14.  
  15. const Button = styled.button`
  16. font-size: 1em;
  17. margin: 1em;
  18. padding: 0.25em 1em;
  19. border-radius: 5px;
  20. color: palevioletred;
  21. border: 2px solid palevioletred;
  22. cursor: pointer;
  23.  
  24. ${props =>
  25. props.primary &&
  26. css`
  27. border: 2px solid mediumseagreen;
  28. color: mediumseagreen;
  29. `}
  30. `
  31.  

5、样式的继承

  1. import React, { Component } from 'react'
  2. import styled from 'styled-components'
  3.  
  4. export default class Style3 extends Component {
  5. render() {
  6. return (
  7. <div>
  8. <Button> 提交 </Button>
  9. <ExtendingButton> 提交 </ExtendingButton>
  10. </div>
  11. )
  12. }
  13. }
  14.  
  15. const Button = styled.button`
  16. background: palevioletred;
  17. color: white;
  18. `
  19.  
  20. const ExtendingButton = styled(Button)`
  21. font-size: 18px;
  22. padding: 5px 25px;
  23. `
  24.  

四、使用sass

使用create-react-app创建的项目是支持sass的但是需要自己安装

1、安装

  1. npm install node-sass

2、直接使用

  1. import React, { Component } from 'react'
  2. import './style4.scss'
  3.  
  4. export default class Style4 extends Component {
  5. render() {
  6. return (
  7. <div>
  8. <div className="title">我是标题</div>
  9. </div>
  10. )
  11. }
  12. }
  13.  

五、使用css-module

使用create-react-app创建的项目,默认情况下就支持css-module

1、样式文件必须以[name].module.css或[name].module.scss的形式命名

2、以变量的形式导入样式文件,比如 import styles from './style.module.css';

3、className以变量引用的方式添加,比如 className={ styles.title }

  1. import React, { Component } from 'react'
  2. import styles from './Style5.module.scss'
  3.  
  4. export default class Style5 extends Component {
  5. render() {
  6. return (
  7. <div>
  8. <div className={styles.title}>我是标题</div>
  9. </div>
  10. )
  11. }
  12. }
  13. <div class="Style5_title__lsl4D"></div>
  14.  

4、如果不想使用默认的哈希值

  1. :global(.wrap) {
  2. color: green;
  3. }
  4. // 直接使用
  5. <h2 className="wrap">你好</h2>

5、样式的继承

  1. .className {
  2. color: green;
  3. background: red;
  4. }
  5.  
  6. .otherClassName {
  7. composes: className; // 直接继承上面的
  8. color: yellow;
  9. }
  10.  
  11. .title {
  12. composes: className from './another.css'; // 直接使用外部样式表
  13. color: red;
  14. }
  15.  

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持w3xue。

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号