国产99久久精品_欧美日本韩国一区二区_激情小说综合网_欧美一级二级视频_午夜av电影_日本久久精品视频

最新文章專題視頻專題問答1問答10問答100問答1000問答2000關(guān)鍵字專題1關(guān)鍵字專題50關(guān)鍵字專題500關(guān)鍵字專題1500TAG最新視頻文章推薦1 推薦3 推薦5 推薦7 推薦9 推薦11 推薦13 推薦15 推薦17 推薦19 推薦21 推薦23 推薦25 推薦27 推薦29 推薦31 推薦33 推薦35 推薦37視頻文章20視頻文章30視頻文章40視頻文章50視頻文章60 視頻文章70視頻文章80視頻文章90視頻文章100視頻文章120視頻文章140 視頻2關(guān)鍵字專題關(guān)鍵字專題tag2tag3文章專題文章專題2文章索引1文章索引2文章索引3文章索引4文章索引5123456789101112131415文章專題3
問答文章1 問答文章501 問答文章1001 問答文章1501 問答文章2001 問答文章2501 問答文章3001 問答文章3501 問答文章4001 問答文章4501 問答文章5001 問答文章5501 問答文章6001 問答文章6501 問答文章7001 問答文章7501 問答文章8001 問答文章8501 問答文章9001 問答文章9501
當(dāng)前位置: 首頁 - 科技 - 知識百科 - 正文

React中使用外部樣式的3種方式(小結(jié))

來源:懂視網(wǎng) 責(zé)編:小采 時間:2020-11-27 21:56:07
文檔

React中使用外部樣式的3種方式(小結(jié))

React中使用外部樣式的3種方式(小結(jié)):一、關(guān)于css-in-js的認識 1、css-in-js是一種使用 js 編寫 css 樣式的 css 處理方案。它的實現(xiàn)方案有很多,比如styled-components、polished、glamorous(paypal 開源的,不再維護)、radium、emotion等等。 2、其中最成熟的便是styled-
推薦度:
導(dǎo)讀React中使用外部樣式的3種方式(小結(jié)):一、關(guān)于css-in-js的認識 1、css-in-js是一種使用 js 編寫 css 樣式的 css 處理方案。它的實現(xiàn)方案有很多,比如styled-components、polished、glamorous(paypal 開源的,不再維護)、radium、emotion等等。 2、其中最成熟的便是styled-

一、關(guān)于css-in-js的認識

1、css-in-js是一種使用 js 編寫 css 樣式的 css 處理方案。它的實現(xiàn)方案有很多,比如styled-components、polished、glamorous(paypal 開源的,不再維護)、radium、emotion等等。

2、其中最成熟的便是styled-components和emotion。它們真正意義上實現(xiàn)了組件化style,可以說是專門為 react 打造的。

二、styled-components 簡介

styled-components是 css-in-js 主流的實現(xiàn)方案,同時也是組件化style的主流實現(xiàn)方案。

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

1、唯一class類名:和 css-module 異曲同工,生成唯一類名,避免重復(fù)和全局污染,也不需要你費腦筋思考該如何命名。

2、無冗余css代碼:它的樣式和組件綁定,組件調(diào)用則樣式起作用。意味著你不需要關(guān)心如何檢測和刪除那些未使用的 css 代碼。

3、動態(tài)樣式: 它可以很簡單地調(diào)整和拓展組件的樣式,而不需要建立很多個 class 類來維護組件的樣式。

4、自動添加兼容前綴:和 Autoprefixer 類似,會自動添加瀏覽器兼容前綴以支持舊版瀏覽器。

5、支持變量和繼承:你可以使用變量來設(shè)置不同的樣式,使用這些不同樣式時只需要給樣式組件傳遞一個參數(shù)即可。

三、styled-components使用方式

1、安裝

npm install styled-components

2、使用

styled-components主要基于 es6 的標(biāo)簽?zāi)0逭Z法調(diào)用標(biāo)簽函數(shù)

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

export default class Style extends Component {
 render() {
 return (
 <>
 <div>
 <Title>我是標(biāo)題</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">我是內(nèi)容</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傳遞參數(shù)的方式

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創(chuàng)建的項目是支持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">我是標(biāo)題</div>
 </div>
 )
 }
}

五、使用css-module

使用create-react-app創(chuàng)建的項目,默認情況下就支持css-module

1、樣式文件必須以[name].module.css或[name].module.scss的形式命名

2、以變量的形式導(dǎo)入樣式文件,比如 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}>我是標(biāo)題</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;
}

聲明:本網(wǎng)頁內(nèi)容旨在傳播知識,若有侵權(quán)等問題請及時與本網(wǎng)聯(lián)系,我們將在第一時間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com

文檔

React中使用外部樣式的3種方式(小結(jié))

React中使用外部樣式的3種方式(小結(jié)):一、關(guān)于css-in-js的認識 1、css-in-js是一種使用 js 編寫 css 樣式的 css 處理方案。它的實現(xiàn)方案有很多,比如styled-components、polished、glamorous(paypal 開源的,不再維護)、radium、emotion等等。 2、其中最成熟的便是styled-
推薦度:
標(biāo)簽: 外部 使用的 方式
  • 熱門焦點

最新推薦

猜你喜歡

熱門推薦

專題
Top
主站蜘蛛池模板: 99精品视频在线观看免费播放 | 日韩欧美大陆 | 在线不欧美| 欧美国产成人精品一区二区三区 | 欧美日韩ay在线观看 | 免费观看国产一区二区三区 | 国产成人欧美一区二区三区的 | 一区二区三区在线免费视频 | 91久久精品国产91久久性色也 | 亚洲成a人一区二区三区 | 天码毛片一区二区三区入口 | 国产欧美日韩综合精品无毒 | 国产全部视频 | 亚洲欧洲免费视频 | 国产一区二区精品久久91 | 国产特级全黄一级毛片不卡 | 亚洲国产精久久久久久久 | 精品国产欧美一区二区五十路 | 手机看片日韩欧美 | 国产毛片久久久久久国产毛片 | 一区二区三区观看 | 久久精品欧美日韩精品 | 亚洲欧美日韩精品永久在线 | 91精品一区二区三区在线观看 | 亚洲 欧美 国产另类首页 | 久久精品国产免费中文 | 欧美综合国产精品日韩一 | 久久99九九精品免费 | 一区二区三区高清不卡 | 色婷婷综合在线 | 亚洲精品98久久久久久中文字幕 | 欧美极品在线视频 | 免费在线观看亚洲 | 国产一区二区三区免费观看 | 国产成人亚洲精品影院 | 国产日韩一区二区三区在线观看 | 精品日韩一区二区 | 日韩欧美国产另类 | 欧美另类激情 | 99热这里只有精品一区二 | 久久亚洲国产成人影院 |