React中使用外部样式的3中方式

栏目: 服务器 · 发布时间: 5年前

内容简介:下面是使用

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

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

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

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

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

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

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

三、 styled-components 使用方式

  • 1、安装

    npm install styled-components
    复制代码
  • 2、使用

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

    import React, { Component } from 'react'
    import styled from 'styled-components'
    
    export default class Style extends Component {
      render() {
        return (
          <>
            <div>
              <Title>我是标题</Title>
            </div>
          </>
        )
      }
    }
    
    // 使用es6的模板字符串的方式(下面表示定义了h1的样式)
    const Title = styled.h1`
      font-size: 20px;
      color: #f00;
    `
    复制代码
  • 3、嵌套的使用

    import React, { Component } from 'react'
    import styled from 'styled-components'
    
    export default class Style extends Component {
      render() {
        return (
          <>
            <div>
              <Content>
                <h2>你好</h2>
                <div className="content">我是内容</div>
              </Content>
            </div>
          </>
        )
      }
    }
    
    const Content = styled.div`
      width: 100%;
      height: 500px;
      border: 1px solid #f00;
      > h2 {
        color: pink;
      }
      > .content {
        text-align: center;
        color: #f00;
      }
    `
    复制代码
  • 4、使用 props 传递参数的方式

    import React, { Component } from 'react'
    import styled, { css } from 'styled-components'
    
    export default class Style2 extends Component {
      render() {
        return (
          <div>
            <Button> 提交 </Button>
            <Button primary> 提交 </Button>
          </div>
        )
      }
    }
    
    const Button = styled.button`
      font-size: 1em;
      margin: 1em;
      padding: 0.25em 1em;
      border-radius: 5px;
      color: palevioletred;
      border: 2px solid palevioletred;
      cursor: pointer;
    
      ${props =>
        props.primary &&
        css`
          border: 2px solid mediumseagreen;
          color: mediumseagreen;
        `}
    `
    复制代码
  • 5、样式的继承

import React, { Component } from 'react'
import styled from 'styled-components'

export default class Style3 extends Component {
  render() {
    return (
      <div>
        <Button> 提交 </Button>
        <ExtendingButton> 提交 </ExtendingButton>
      </div>
    )
  }
}

const Button = styled.button`
  background: palevioletred;
  color: white;
`

const ExtendingButton = styled(Button)`
  font-size: 18px;
  padding: 5px 25px;
`
复制代码

四、使用 sass

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

  • 1、安装

    npm install node-sass
    复制代码
  • 2、直接使用

    import React, { Component } from 'react'
    import './style4.scss'
    
    export default class Style4 extends Component {
      render() {
        return (
          <div>
            <div className="title">我是标题</div>
          </div>
        )
      }
    }
    复制代码

五、使用 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 }

    import React, { Component } from 'react'
    import styles from './Style5.module.scss'
    
    export default class Style5 extends Component {
      render() {
        return (
          <div>
            <div className={styles.title}>我是标题</div>
          </div>
        )
      }
    }
    复制代码
    <div class="Style5_title__lsl4D"></div>
    复制代码
  • 4、如果不想使用默认的哈希值

    :global(.wrap) {
      color: green;
    }
    复制代码
    // 直接使用
    <h2 className="wrap">你好</h2>
    复制代码
  • 5、样式的继承

    .className {
      color: green;
      background: red;
    }
    
    .otherClassName {
      composes: className; // 直接继承上面的
      color: yellow;
    }
    
    .title {
      composes: className from './another.css'; // 直接使用外部样式表
      color: red;
    }
    复制代码

以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

知识发现

知识发现

史忠植 / 2011-1 / 59.00元

《知识发现(第2版)》全面而又系统地介绍了知识发现的方法和技术,反映了当前知识发现研究的最新成果和进展。全书共分15章。第1章是绪论,概述知识发现的重要概念和发展过程。下面三章重点讨论分类问题,包括决策树、支持向量机和迁移学习。第5章阐述聚类分析。第6章是关联规则。第7章讨论粗糙集和粒度计算。第8章介绍神经网络,书中着重介绍几种实用的算法。第9章探讨贝叶斯网络。第10章讨论隐马尔可夫模型。第11章......一起来看看 《知识发现》 这本书的介绍吧!

随机密码生成器
随机密码生成器

多种字符组合密码

XML 在线格式化
XML 在线格式化

在线 XML 格式化压缩工具

HSV CMYK 转换工具
HSV CMYK 转换工具

HSV CMYK互换工具