天天看點

7種在React中使用CSS的方式

7種在React中使用CSS的方式

第一種:在元件中直接使用style

不需要元件從外部約會css檔案,直接在元件中書寫。

import react, { Component } from "react";


const div1 = {
  width: "300px",
  margin: "30px auto",
  backgroundColor: "#44014C",  //駝峰法
  minHeight: "200px",
  boxSizing: "border-box"
};


class Test extends Component {
  constructor(props, context) {
    super(props);
  }


  render() {
    return (
     <div style={div1}>123</div>
     <div style="background-color:red;">
    );
  }
}


export default Test;      

注意事項:

  1. 在正常的css中,某種背景色,box-sizing等屬性,在style對象div1中的屬性中,必須轉換成駝峰法,backgroundColor,boxSizing。而沒有連字元的屬性,如margin,width等,則在style對象中不變。
  2. 在正常的css中,css的值不需要用雙引好(“”),如
.App-header {
  background-color: #282c34;
  min-height: 100vh;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  font-size: calc(10px + 2vmin);
  color: white;
}      

而在react中使用style對象的方式時。值必須用雙引号包裹起來。

這種方式的反應樣式,隻作用于目前元件。

第二種:在元件中約會[name] .css檔案

需要在目前元件開頭使用導入約會css檔案。

import React, { Component } from "react";
import TestChidren from "./TestChidren";
import "@/assets/css/index.scss";


class Test extends Component {
  constructor(props, context) {
    super(props);
  }


  render() {
    return (
      <div>
        <div className="link-name">123</div>
        <TestChidren>測試子元件的樣式</TestChidren>
      </div>


    );
  }
}


export default Test;      

這種方式約會的css樣式,會作用于目前元件及其所有後代元件。

第三種:在元件中約會[name] .scss檔案

datereact内部已經支援了字尾為scss的檔案,是以隻需要安裝node-sass就可以,因為有個node-sass,scss檔案才能在node環境上編譯成css檔案。

>yarn add node-sass      

然後編寫scss檔案

//index.scss
.App{
  background-color: #282c34;
  .header{
    min-height: 100vh;
    color: white;
  }
}      

關于如何詳細的使用sass,請檢視sass官網

這種方式約會的css樣式,同樣會作用于目前元件及其所有後代元件。

第四種:在元件中約會[name] .module.css檔案

将css檔案作為一個子產品約會,這個子產品中的所有css,隻作用于電流元件。不會影響電流元件的後代元件。

import React, { Component } from "react";
import TestChild from "./TestChild";
import moduleCss from "./test.module.css";


class Test extends Component {
  constructor(props, context) {
    super(props);
  }  


  render() {
    return (
     <div>
       <div className={moduleCss.linkName}>321321</div>
       <TestChild></TestChild>
     </div>
    );
  }
}


export default Test;      

這種方式可以看做是前面第一種在元件中使用style的更新版。完全将css群組件分離開,又不會影響其他元件。

第五種:在元件中約會[name] .module.scss檔案

某種第四種,差別是第四種約會css子產品,而這種是約會scss子產品而已。

import React, { Component } from "react";
import TestChild from "./TestChild";
import moduleCss from "./test.module.scss";


class Test extends Component {
  constructor(props, context) {
    super(props);
  }  


  render() {
    return (
     <div>
       <div className={moduleCss.linkName}>321321</div>
       <TestChild></TestChild>
     </div>
    );
  }
}


export default Test;      

同樣這種方式可以看做是前面第一種在元件中使用style的更新版。

第六種:使用styled-components

需要先安裝

>yarn add styled-components      

然後建立一個js檔案(注意是js檔案,不是css檔案)

//style.js
import styled, { createGlobalStyle } from "styled-components";


export const SelfLink = styled.div`
  height: 50px;
  border: 1px solid red;
  color: yellow;
;


export const SelfButton = styled.div`
  height: 150px;
  width: 150px;
  color: ${props => props.color};
  background-image: url(${props => props.src});
  background-size: 150px 150px;
;      

元件中使用styled-components樣式

import React, { Component } from "react";


import { SelfLink, SelfButton } from "./style";


class Test extends Component {
  constructor(props, context) {
    super(props);
  }  


  render() {
    return (
     <div>
       <SelfLink title="People's Republic of China">app.js</SelfLink>
       <SelfButton color="palevioletred" style={{ color: "pink" }} src={fist}>
          SelfButton
        </SelfButton>
     </div>
    );
  }
}


export default Test;      

這種方式是将整個的CSS樣式,和HTML節點整體合并成一個元件。引入這個元件的HTML和CSS都有了。

它的好處在于可以随時通過往元件上傳入屬性,來動态的改變樣式。對于處理變量,媒體查詢,僞類等較友善的。

這種方式的css也隻對目前元件有效。

具體用法,請檢視styled-components官網

第七種:使用radium

需要先安裝

>yarn add radium      

然後在react元件中直接約會使用​

import React, { Component } from "react";
import Radium from 'radium';


let styles = {
  base: {
    color: '#fff',
    ':hover': {
      background: '#0074d9'
    }
  },
  primary: {
    background: '#0074D9'
  },
  warning: {
    background: '#FF4136'
  }
};


class Test extends Component {
  constructor(props, context) {
    super(props);
  }  


  render() {
    return (
     <div>
      <button style={[ styles.base, styles.primary ]}>
        this is a primary button
      </button>
     </div>
    );
  }
}


export default Radium(Test);      

對于處理變量,媒體查詢,僞類等是不友善的。

使用Radium可以直接處理變量,媒體查詢,僞類等,并且可以直接使用js中的數學,連接配接,正規表達式,條件,函數等。

具體用法請檢視radium官網

注意:

在export之前,必須用Radium包裹。

本文完~