国产片侵犯亲女视频播放_亚洲精品二区_在线免费国产视频_欧美精品一区二区三区在线_少妇久久久_在线观看av不卡

服務器之家:專注于服務器技術及軟件下載分享
分類導航

node.js|vue.js|jquery|angularjs|React|json|js教程|

服務器之家 - 編程語言 - JavaScript - React - React使用emotion寫css代碼

React使用emotion寫css代碼

2022-02-25 16:10joychenke React

這篇文章主要介紹了React如何使用emotion寫css代碼,幫助大家更好的理解和學習使用React,感興趣的朋友可以了解下

簡介:

emotion是一個JavaScript庫,使用emotion可以用寫js的方式寫css代碼。在react中安裝emotion后,可以很方便進行css的封裝,復用。使用emotion后,瀏覽器渲染出來的標簽是會加上一個css開頭的標識。如下:截圖中以css-開頭的幾個標簽,就是使用emotion庫后渲染出來的。

React使用emotion寫css代碼

下面就從安裝到使用,介紹下emotion在工程中的應用。

emotion的安裝:

?
1
2
yarn add @emotion/react
yarn add @emotion/styled

新增普通css組件:

1,命名和組件一樣,大寫字母開頭
2,styled后面跟html標簽

?
1
2
// 引入emotion
import styled from "@emotion/styled”;
?
1
2
3
4
5
6
7
// 使用emotion 創建css組件
const Container = styled.div`
display: flex;
flex-direction: column;
align-items: center;
min-height: 100vh;
`;
?
1
2
3
4
//在html代碼中使用css組件:
<Container>
// html代碼
</Container>

給已存在組件加樣式:

1,變量名首字符大寫
2,將已經存在的組件作為參數傳入styled
3,樣式代碼可以加參數

?
1
2
3
4
5
6
7
8
9
10
// Card 是antd已存在的組件
const ShadowCard = styled(Card)`
    width: 40rem;
    min-height: 56rem;
    padding: 3.2rem 4rem;
    border-radius: 0.3rem;
    box-sizing: border-box;
    box-shadow: rgba(0, 0, 0, 0.1) 0 0 10px;
    text-align: center;
`;
?
1
2
3
4
5
6
7
8
9
10
// 引入的圖片,作為參數直接使用
import logo from "assets/logo.svg";
 
// 反引號可參照魔法字符串傳入參數
const Header = styled.header`
background: url(${logo}) no-repeat center;
padding: 5rem 0;
background-size: 8rem;
width: 100%;
`;

提取公共的css組件

1, 反引號之前,接收泛型的參數, 可能用到的參數都要列出來
2, 取傳進來的參數,用props來取,比如props.between, 可以用函數返回值給css屬性賦值,css屬性不渲染,返回值就是undefined

?
1
justify-content: ${(props) => (props.between ? "space-between" : undefined)};

3, 可以用css選擇器
4,調用時,跟普通js組件一樣使用,傳參也相同

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
// 調用Row組件
<HeaderLeft gap={1}>
//html代碼
</HeaderLeft>
const HeaderLeft = styled(Row)``;
 
 
// 定義Row組件
export const Row = styled.div<{
  gap?: number | boolean;
  between?: Boolean;
  marginBottom?: number;
}>`
display: flex;
align-items: center;
justify-content: ${(props) => (props.between ? "space-between" : undefined)};
 
margin-bottom: ${(props) =>
 
props.marginBottom ? props.marginBottom + "px" : undefined};
 
> * {
margin-top: 0 !important;
margin-bottom: 0 !important;
margin-right: ${(props) =>
 
typeof props.gap === "number"
    ? props.gap + "rem"
    : props.gap
    ? "2rem"
    : undefined};
}
`;

寫emotion行內樣式

1,在組件的頂部寫上 下面代碼,告知當前組件用了emotion行內樣式

?
1
/* @jsxImportSource @emotion/react */

2,行內樣式的格式:css={ /* 行內樣式代碼 */ }

?
1
2
3
<Form css={{ marginBottom: "2rem" }} layout={"inline”}>
// html代碼
</Form>

以上就是emotion的介紹和使用。(#^.^#)

以上就是React使用emotion寫css代碼的詳細內容,更多關于React用emotion寫css代碼的資料請關注服務器之家其它相關文章!

原文鏈接:https://segmentfault.com/a/1190000039778793

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 日韩欧美手机在线 | 国产一级成人 | 中文字幕久久网 | 天天干夜夜操 | 欧美中文字幕 | 欧美日韩电影一区 | 色国产精品 | 久久久国产一区二区三区四区小说 | 人一级毛片 | 夜夜春精品视频高清69式 | 久久三区| 欧美精品99| 久久精品欧美 | 精品综合 | 国产精品久久久久久久久小说 | 国产精品毛片一区视频播不卡 | 精品久久久久久久久久久久久久 | 久草视频网站 | 91精品国产一区二区三区香蕉 | 精品一区二区免费视频视频 | 亚洲欧美激情精品一区二区 | 久久久久av| 国产婷婷色一区二区三区 | 婷婷在线免费视频 | 免费不卡视频 | 免费一级性片 | 亚洲精品日韩综合观看成人91 | 精品免费av | 亚洲天堂中文字幕 | 欧美视频一二三区 | 欧美精品 | 好看毛片 | 一区二区欧美视频 | 四虎成人免费电影 | 久艹精品 | 国产最好的精华液网站 | 欧美日韩精品在线观看 | 欧美极品一区二区 | k8久久久一区二区三区 | 日韩一区二区在线播放 | 在线激情网 |