欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

react中使用css的7中方式(最全總結(jié))

 更新時(shí)間:2019年02月11日 09:23:25   作者:小李子的一天  
這篇文章主要介紹了react中使用css的7中方式(最全總結(jié)),小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧

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

不需要組件從外部引入css文件,直接在組件中書(shū)寫(xiě)。

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;

注意事項(xiàng):

  1. 在正常的css中,比如background-color,box-sizing等屬性,在style對(duì)象div1中的屬性中,必須轉(zhuǎn)換成駝峰法,backgroundColor,boxSizing。而沒(méi)有連字符的屬性,如margin,width等,則在style對(duì)象中不變。
  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對(duì)象的方式時(shí)。值必須用雙引號(hào)包裹起來(lái)。

這種方式的react樣式,只作用于當(dāng)前組件。

第二種: 在組件中引入[name].css文件

需要在當(dāng)前組件開(kāi)頭使用import引入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>測(cè)試子組件的樣式</TestChidren>
   </div>

  );
 }
}

export default Test;

這種方式引入的css樣式,會(huì)作用于當(dāng)前組件及其所有后代組件。

第三種: 3、在組件中引入[name].scss文件

引入react內(nèi)部已經(jīng)支持了后綴為scss的文件,所以只需要安裝node-sass即可,因?yàn)橛袀€(gè)node-sass,scss文件才能在node環(huán)境上編譯成css文件。

>yarn add node-sass

然后編寫(xiě)scss文件

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

關(guān)于如何詳細(xì)的使用sass,請(qǐng)查看sass官網(wǎng)

這種方式引入的css樣式,同樣會(huì)作用于當(dāng)前組件及其所有后代組件。

第四種: 在組件中引入[name].module.css文件

將css文件作為一個(gè)模塊引入,這個(gè)模塊中的所有css,只作用于當(dāng)前組件。不會(huì)影響當(dāng)前組件的后代組件。

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的升級(jí)版。完全將css和組件分離開(kāi),又不會(huì)影響其他組件。

第五種: 在組件中引入 [name].module.scss文件

類似于第四種,區(qū)別是第四種引入css module,而這種是引入 scss module而已。

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的升級(jí)版。

第六種: 使用styled-components

需要先安裝

>yarn add styled-components

然后創(chuàng)建一個(gè)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;

這種方式是將整個(gè)css樣式,和html節(jié)點(diǎn)整體合并成一個(gè)組件。引入這個(gè)組件html和css都有了。
它的好處在于可以隨時(shí)通過(guò)往組件上傳入 屬性,來(lái)動(dòng)態(tài)的改變樣式。對(duì)于處理變量、媒體查詢、偽類等較方便的。

這種方式的css也只對(duì)當(dāng)前組件有效。

具體用法,請(qǐng)查看styled-components官網(wǎng)

第七種: 使用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); 

對(duì)于處理變量、媒體查詢、偽類等是不方便的。

使用Radium可以直接處理變量、媒體查詢、偽類等,并且可以直接使用js中的數(shù)學(xué),連接,正則表達(dá)式,條件,函數(shù)等。

具體用法請(qǐng)查看radium官網(wǎng)

注意:

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

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • React腳手架搭建的學(xué)習(xí)

    React腳手架搭建的學(xué)習(xí)

    本文主要介紹了React腳手架搭建的學(xué)習(xí),文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-12-12
  • React-Hooks之useImperativeHandler使用介紹

    React-Hooks之useImperativeHandler使用介紹

    這篇文章主要為大家介紹了React-Hooks之useImperativeHandler使用介紹,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-07-07
  • React+Webpack快速上手指南(小結(jié))

    React+Webpack快速上手指南(小結(jié))

    這篇文章主要介紹了React+Webpack快速上手指南(小結(jié)),小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2018-08-08
  • 簡(jiǎn)談創(chuàng)建React Component的幾種方式

    簡(jiǎn)談創(chuàng)建React Component的幾種方式

    這篇文章主要介紹了創(chuàng)建React Component的幾種方式,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,,需要的朋友可以參考下
    2019-06-06
  • react+antd.3x實(shí)現(xiàn)ip輸入框

    react+antd.3x實(shí)現(xiàn)ip輸入框

    這篇文章主要為大家詳細(xì)介紹了react+antd.3x實(shí)現(xiàn)ip輸入框,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-10-10
  • React Hook 'useEffect' is called in function報(bào)錯(cuò)解決

    React Hook 'useEffect' is call

    這篇文章主要為大家介紹了React Hook 'useEffect' is called in function報(bào)錯(cuò)解決,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-12-12
  • React之PureComponent的使用作用

    React之PureComponent的使用作用

    這篇文章主要介紹了React之PureComponent的使用作用,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2018-07-07
  • React如何避免子組件無(wú)效刷新

    React如何避免子組件無(wú)效刷新

    這篇文章主要介紹了React幾種避免子組件無(wú)效刷新的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-03-03
  • React使用react-sortable-hoc如何實(shí)現(xiàn)拖拽效果

    React使用react-sortable-hoc如何實(shí)現(xiàn)拖拽效果

    這篇文章主要介紹了React使用react-sortable-hoc如何實(shí)現(xiàn)拖拽效果問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-07-07
  • React中使用Mobx的方法

    React中使用Mobx的方法

    Mobx是一個(gè)前端“狀態(tài)管理框架”,狀態(tài)管理就是將分布在各個(gè)組件、各個(gè)模塊中的狀態(tài)的變化,按照一定的規(guī)則,進(jìn)行統(tǒng)一的管理,這篇文章主要介紹了React中如何使用Mobx,需要的朋友可以參考下
    2023-02-02

最新評(píng)論