国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > JavaScript > 正文

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

2019-11-19 11:27:09
字體:
來源:轉載
供稿:網友

一、關于css-in-js的認識

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

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

二、styled-components 簡介

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

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

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

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

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

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

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

三、styled-components使用方式

1、安裝

npm install styled-components

2、使用

styled-components主要基于 es6 的標簽模板語法調用標簽函數

import React, { Component } from 'react'import styled from 'styled-components'export default class Style extends Component { render() {  return (   <>    <div>     <Title>我是標題</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">我是內容</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傳遞參數的方式

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創建的項目是支持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">我是標題</div>   </div>  ) }}

五、使用css-module

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

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

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

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持武林網。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 常熟市| 水富县| 酒泉市| 涡阳县| 温宿县| 濉溪县| 苍南县| 柳林县| 青川县| 西乡县| 历史| 临海市| 房产| 天镇县| 昆山市| 宁德市| 南陵县| 银川市| 云和县| 和顺县| 通海县| 南木林县| 墨竹工卡县| 天台县| 汉川市| 高平市| 册亨县| 旅游| 临潭县| 奈曼旗| 穆棱市| 佳木斯市| 茌平县| 武汉市| 沙洋县| 乌兰察布市| 克什克腾旗| 固原市| 高雄市| 綦江县| 贵德县|