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;
    }
    複製代碼
相關文章
相關標籤/搜索