這篇文章主要介紹react寫style的方法,文中介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們一定要看完!
react寫style的方法:1、使用內(nèi)聯(lián)式;2、使用className方法;3、使用classnames動態(tài)修改樣式;4、使用【styled-components】插件寫標簽樣式。
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;
這種寫法不推薦使用,樣式多了之后,會導致代碼比較亂!
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;
新建一個.css
文件,將文件引進來,標簽中使用className=“textColor”
,就可以使用引入.css文件中類為’textColor’的樣式了.一般的項目用這個方式就可以了.
3、使用classnames動態(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;
這種動態(tài)修改樣式的方式,需要安裝插件classnames.上面的代碼中,h2標簽的類有textColor和textTitle.項目中一般也會使用.
4、使用styled-components插件寫標簽樣式
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>復習style</Title> </Fragment> ) } } export default Style
使用styled-components
給標簽寫樣式,首先需要安裝該插件.上面的代碼是定義一個Title,通過styled給h2標簽設置樣式,然后在組件中使用的Title就相當于寫過樣式的h2標簽.這種方式在大項目中比較常見.
以上是“react寫style的方法”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對大家有幫助,更多相關知識,歡迎關注創(chuàng)新互聯(lián)行業(yè)資訊頻道!
新聞名稱:react寫style的方法-創(chuàng)新互聯(lián)
標題鏈接:http://jinyejixie.com/article2/dighoc.html
成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供微信小程序、移動網(wǎng)站建設、自適應網(wǎng)站、網(wǎng)站排名、網(wǎng)站導航、定制開發(fā)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉載內(nèi)容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯(lián)