如何在React中調(diào)用外部樣式?很多新手對(duì)此不是很清楚,為了幫助大家解決這個(gè)難題,下面小編將為大家詳細(xì)講解,有這方面需求的人可以來(lái)學(xué)習(xí)下,希望你能有所收獲。
成都創(chuàng)新互聯(lián)公司堅(jiān)持“要么做到,要么別承諾”的工作理念,服務(wù)領(lǐng)域包括:網(wǎng)站設(shè)計(jì)、成都做網(wǎng)站、企業(yè)官網(wǎng)、英文網(wǎng)站、手機(jī)端網(wǎng)站、網(wǎng)站推廣等服務(wù),滿(mǎn)足客戶(hù)于互聯(lián)網(wǎng)時(shí)代的鹽湖網(wǎng)站設(shè)計(jì)、移動(dòng)媒體設(shè)計(jì)的需求,幫助企業(yè)找到有效的互聯(lián)網(wǎng)解決方案。努力成為您成熟可靠的網(wǎng)絡(luò)建設(shè)合作伙伴!一、關(guān)于css-in-js的認(rèn)識(shí)
1、css-in-js是一種使用 js 編寫(xiě) css 樣式的 css 處理方案。它的實(shí)現(xiàn)方案有很多,比如styled-components、polished、glamorous(paypal 開(kāi)源的,不再維護(hù))、radium、emotion等等。
2、其中最成熟的便是styled-components和emotion。它們真正意義上實(shí)現(xiàn)了組件化style,可以說(shuō)是專(zhuān)門(mén)為 react 打造的。
二、styled-components 簡(jiǎn)介
styled-components是 css-in-js 主流的實(shí)現(xiàn)方案,同時(shí)也是組件化style的主流實(shí)現(xiàn)方案。
下面是styled-components的一些特性:
1、唯一class類(lèi)名:和 css-module 異曲同工,生成唯一類(lèi)名,避免重復(fù)和全局污染,也不需要你費(fèi)腦筋思考該如何命名。
2、無(wú)冗余css代碼:它的樣式和組件綁定,組件調(diào)用則樣式起作用。意味著你不需要關(guān)心如何檢測(cè)和刪除那些未使用的 css 代碼。
3、動(dòng)態(tài)樣式: 它可以很簡(jiǎn)單地調(diào)整和拓展組件的樣式,而不需要建立很多個(gè) class 類(lèi)來(lái)維護(hù)組件的樣式。
4、自動(dòng)添加兼容前綴:和 Autoprefixer 類(lèi)似,會(huì)自動(dòng)添加瀏覽器兼容前綴以支持舊版瀏覽器。
5、支持變量和繼承:你可以使用變量來(lái)設(shè)置不同的樣式,使用這些不同樣式時(shí)只需要給樣式組件傳遞一個(gè)參數(shù)即可。
三、styled-components使用方式
1、安裝
npm install styled-components
2、使用
styled-components主要基于 es6 的標(biāo)簽?zāi)0逭Z(yǔ)法調(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的模板字符串的方式(下面表示定義了h2的樣式) const Title = styled.h2` 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> <h3>你好</h3> <div className="content">我是內(nèi)容</div> </Content> </div> </> ) } } const Content = styled.div` width: 100%; height: 500px; border: 1px solid #f00; > h3 { 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)建的項(xià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)建的項(xiàng)目,默認(rèn)情況下就支持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、如果不想使用默認(rèn)的哈希值
:global(.wrap) { color: green; } // 直接使用 <h3 className="wrap">你好</h3>
5、樣式的繼承
.className { color: green; background: red; } .otherClassName { composes: className; // 直接繼承上面的 color: yellow; } .title { composes: className from './another.css'; // 直接使用外部樣式表 color: red; }
看完上述內(nèi)容是否對(duì)您有幫助呢?如果還想對(duì)相關(guān)知識(shí)有進(jìn)一步的了解或閱讀更多相關(guān)文章,請(qǐng)關(guān)注創(chuàng)新互聯(lián)成都網(wǎng)站設(shè)計(jì)公司行業(yè)資訊頻道,感謝您對(duì)創(chuàng)新互聯(lián)成都網(wǎng)站設(shè)計(jì)公司的支持。
另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務(wù)器15元起步,三天無(wú)理由+7*72小時(shí)售后在線(xiàn),公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、網(wǎng)站設(shè)計(jì)器、香港服務(wù)器、美國(guó)服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡(jiǎn)單易用、服務(wù)可用性高、性?xún)r(jià)比高”等特點(diǎn)與優(yōu)勢(shì),專(zhuān)為企業(yè)上云打造定制,能夠滿(mǎn)足用戶(hù)豐富、多元化的應(yīng)用場(chǎng)景需求。
當(dāng)前文章:如何在React中調(diào)用外部樣式-創(chuàng)新互聯(lián)
瀏覽路徑:http://bm7419.com/article20/ddhdjo.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供軟件開(kāi)發(fā)、動(dòng)態(tài)網(wǎng)站、標(biāo)簽優(yōu)化、做網(wǎng)站、品牌網(wǎng)站制作、外貿(mào)網(wǎng)站建設(shè)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶(hù)投稿、用戶(hù)轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話(huà):028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容