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

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

  • 二、其中最成熟的即是styled-componentsemotion。它们真正意义上实现了组件化style,能够说是专门为 react 打造的。

2、styled-components 简介

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

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

  • 一、惟一class类名:和 css-module 殊途同归,生成惟一类名,避免重复和全局污染,也不须要你费脑筋思考该如何命名。node

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

  • 三、动态样式: 它能够很简单地调整和拓展组件的样式,而不须要创建不少个 class 类来维护组件的样式。git

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

  • 五、支持变量和继承:你能够使用变量来设置不一样的样式,使用这些不一样样式时只须要给样式组件传递一个参数便可。github

3、styled-components使用方式

  • 一、安装shell

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

    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; ` 复制代码
  • 三、嵌套的使用

    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; } ` 复制代码
  • 四、使用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; `} `
    复制代码
  • 五、样式的继承

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; `
复制代码

4、使用sass

使用create-react-app建立的项目是支持sass的可是须要本身安装

  • 一、安装

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

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

5、使用css-module

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

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

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

  • 三、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>
    复制代码
  • 四、若是不想使用默认的哈希值

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

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