網(wǎng)站首頁(yè) 編程語(yǔ)言 正文
前言
組件式開(kāi)發(fā)選擇合適的css
解決方案尤為重要
通常會(huì)遵循以下規(guī)則:
- 可以編寫(xiě)局部css,不會(huì)隨意污染其他組件內(nèi)的原生;
- 可以編寫(xiě)動(dòng)態(tài)的css,可以獲取當(dāng)前組件的一些狀態(tài),根據(jù)狀態(tài)的變化生成不同的css樣式;
- 支持所有的css特性:偽類(lèi)、動(dòng)畫(huà)、媒體查詢等;
- 編寫(xiě)起來(lái)簡(jiǎn)潔方便、最好符合一貫的css風(fēng)格特點(diǎn)
在這一方面,vue
使用css
起來(lái)更為簡(jiǎn)潔:
- 通過(guò) style 標(biāo)簽編寫(xiě)樣式
- scoped 屬性決定編寫(xiě)的樣式是否局部有效
- lang 屬性設(shè)置預(yù)處理器
- 內(nèi)聯(lián)樣式風(fēng)格的方式來(lái)根據(jù)最新?tīng)顟B(tài)設(shè)置和改變css
而在react
中,引入CSS
就不如Vue
方便簡(jiǎn)潔,其引入css
的方式有很多種,各有利弊
方式
常見(jiàn)的CSS
引入方式有以下:
- 在組件內(nèi)直接使用
- 組件中引入 .css 文件
- 組件中引入 .module.css 文件
- CSS in JS
在組件內(nèi)
直接在組件中書(shū)寫(xiě)css
樣式,通過(guò)style
屬性直接引入,如下:
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> <div style={div1}>123</div> <div style={{backgroundColor:"red"}}> </div> ); } } export default Test;
上面可以看到,css
屬性需要轉(zhuǎn)換成駝峰寫(xiě)法 這種方式優(yōu)點(diǎn):
- 內(nèi)聯(lián)樣式, 樣式之間不會(huì)有沖突
- 可以動(dòng)態(tài)獲取當(dāng)前state中的狀態(tài)
缺點(diǎn):
- 寫(xiě)法上都需要使用駝峰標(biāo)識(shí)
- 某些樣式?jīng)]有提示
- 大量的樣式, 代碼混亂
- 某些樣式無(wú)法編寫(xiě)(比如偽類(lèi)/偽元素)
組件中引入css文件
將css
單獨(dú)寫(xiě)在一個(gè)css
文件中,然后在組件中直接引入
App.css
文件:
.title { color: red; font-size: 20px; } .desc { color: green; text-decoration: underline; }
組件中引入:
import React, { PureComponent } from 'react'; import Home from './Home'; import './App.css'; export default class App extends PureComponent { render() { return ( <div className="app"> <h2 className="title">我是App的標(biāo)題</h2> <p className="desc">我是App中的一段文字描述</p > <Home/> </div> ) } }
這種方式存在不好的地方在于樣式是全局生效,樣式之間會(huì)互相影響
組件中引入 .module.css 文件
將css
文件作為一個(gè)模塊引入,這個(gè)模塊中的所有css
,只作用于當(dāng)前組件。不會(huì)影響當(dāng)前組件的后代組件
這種方式是webpack
特工的方案,只需要配置webpack
配置文件中modules:true
即可
import React, { PureComponent } from 'react'; import Home from './Home'; import './App.module.css'; export default class App extends PureComponent { render() { return ( <div className="app"> <h2 className="title">我是App的標(biāo)題</h2> <p className="desc">我是App中的一段文字描述</p > <Home/> </div> ) } }
這種方式能夠解決局部作用域問(wèn)題,但也有一定的缺陷:
- 引用的類(lèi)名,不能使用連接符(.xxx-xx),在 JavaScript 中是不識(shí)別的
- 所有的 className 都必須使用 {style.className} 的形式來(lái)編寫(xiě)
- 不方便動(dòng)態(tài)來(lái)修改某些樣式,依然需要使用內(nèi)聯(lián)樣式的方式;
CSS in JS
CSS-in-JS, 是指一種模式,其中CSS
由?JavaScript
生成而不是在外部文件中定義
此功能并不是 React 的一部分,而是由第三方庫(kù)提供,例如:
- styled-components
- emotion
- glamorous
下面主要看看styled-components
的基本使用
本質(zhì)是通過(guò)函數(shù)的調(diào)用,最終創(chuàng)建出一個(gè)組件:
- 這個(gè)組件會(huì)被自動(dòng)添加上一個(gè)不重復(fù)的class
- styled-components會(huì)給該class添加相關(guān)的樣式
基本使用如下:
創(chuàng)建一個(gè)style.js
文件用于存放樣式組件:
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; `;
引入樣式組件也很簡(jiǎn)單:
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; 復(fù)制代碼
區(qū)別
通過(guò)上面四種樣式的引入,可以看到:
- 在組件內(nèi)直接使用
css
該方式編寫(xiě)方便,容易能夠根據(jù)狀態(tài)修改樣式屬性,但是大量的演示編寫(xiě)容易導(dǎo)致代碼混亂 - 組件中引入 .css 文件符合我們?nèi)粘5木帉?xiě)習(xí)慣,但是作用域是全局的,樣式之間會(huì)層疊
- 引入.module.css 文件能夠解決局部作用域問(wèn)題,但是不方便動(dòng)態(tài)修改樣式,需要使用內(nèi)聯(lián)的方式進(jìn)行樣式的編寫(xiě)
- 通過(guò)css in js 這種方法,可以滿足大部分場(chǎng)景的應(yīng)用,可以類(lèi)似于預(yù)處理器一樣樣式嵌套、定義、修改狀態(tài)等
至于使用react
用哪種方案引入css
,并沒(méi)有一個(gè)絕對(duì)的答案,可以根據(jù)各自情況選擇合適的方案
原文鏈接:https://juejin.cn/post/7089393721397116958
相關(guān)推薦
- 2022-11-08 hooks中useEffect()使用案例詳解_React
- 2023-03-21 C#中[]的幾種用法示例代碼_C#教程
- 2022-05-18 python必備庫(kù)Matplotlib畫(huà)圖神器_python
- 2022-07-09 Docker網(wǎng)絡(luò)介紹
- 2023-05-24 Python?的第三方調(diào)試庫(kù)????pysnooper???使用示例_python
- 2022-05-31 如何使用正則表達(dá)式判斷郵箱(以C#為例)_C#教程
- 2022-07-14 C++實(shí)現(xiàn)一個(gè)簡(jiǎn)單的線程池的示例代碼_C 語(yǔ)言
- 2022-05-12 Echarts x軸標(biāo)簽太長(zhǎng)解決方案
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲(chǔ)小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過(guò)濾器
- Spring Security概述快速入門(mén)
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡(jiǎn)單動(dòng)態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對(duì)象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支