這篇文章主要介紹react寫style的方法,文中介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們一定要看完!

react寫style的方法:1、使用內(nèi)聯(lián)式;2、使用className方法;3、使用classnames動(dòng)態(tài)修改樣式;4、使用【styled-components】插件寫標(biāo)簽樣式。
react寫style的方法:
1、內(nèi)聯(lián)式
import React, { Fragment } from "react";
class Style extends React.Component {
constructor(props) {
super(props);
}
render() {
const txtColor = {
color: '#F00'
}
return (
<Fragment>
<h2 style={txtColor}></h2>
</Fragment>
);
}
}
export default Style;這種寫法不推薦使用,樣式多了之后,會(huì)導(dǎo)致代碼比較亂!
2、使用className
import React, { Fragment } from "react";
import "./../../style.css";
class Style extends React.Component {
constructor(props) {
super(props);
}
render() {
return (
<Fragment>
<h2 className="textColor"></h2>
</Fragment>
);
}
}
export default Style;新建一個(gè).css文件,將文件引進(jìn)來,標(biāo)簽中使用className=“textColor”,就可以使用引入.css文件中類為’textColor’的樣式了.一般的項(xiàng)目用這個(gè)方式就可以了.
3、使用classnames動(dòng)態(tài)修改樣式
import React, { Fragment } from "react";
import classNames from 'classnames'
class Style extends React.Component {
constructor(props) {
super(props);
}
render() {
return (
<Fragment>
<h2 className={classNames('textColor', {'textContent': false} ,{'textTitle': true})}></h2>
</Fragment>
);
}
}
export default Style;這種動(dòng)態(tài)修改樣式的方式,需要安裝插件classnames.上面的代碼中,h2標(biāo)簽的類有textColor和textTitle.項(xiàng)目中一般也會(huì)使用.
4、使用styled-components插件寫標(biāo)簽樣式
import React, { Fragment } from 'react'
import styled from 'styled-components'
const Title = styled.h2`
color: #f00;
`
class Style extends React.Component {
constructor(props) {
super(props)
}
render() {
return (
<Fragment>
<Title>復(fù)習(xí)style</Title>
</Fragment>
)
}
}
export default Style使用styled-components給標(biāo)簽寫樣式,首先需要安裝該插件.上面的代碼是定義一個(gè)Title,通過styled給h2標(biāo)簽設(shè)置樣式,然后在組件中使用的Title就相當(dāng)于寫過樣式的h2標(biāo)簽.這種方式在大項(xiàng)目中比較常見.
以上是“react寫style的方法”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對(duì)大家有幫助,更多相關(guān)知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!
新聞名稱:react寫style的方法-創(chuàng)新互聯(lián)
標(biāo)題鏈接:http://chinadenli.net/article2/dighoc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供微信小程序、移動(dòng)網(wǎng)站建設(shè)、自適應(yīng)網(wǎng)站、網(wǎng)站排名、網(wǎng)站導(dǎo)航、定制開發(fā)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)