激情久久久_欧美视频区_成人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

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 男女无套免费视频 | 最近国产中文字幕 | 在线免费亚洲 | 毛片在哪看 | 性大片免费看 | 久久伊| 欧美999| 国产呦在线观看视频 | 国产91一区 | 亚洲va久久久噜噜噜久久男同 | 国产亚洲精品久久久久婷婷瑜伽 | 欧美一级毛片欧美一级成人毛片 | aaaaaaa毛片| 欧美精品久久天天躁 | 99激情| 成人三级视频在线观看 | av在线成人 | 一级做受毛片免费大片 | 欧美日韩免费一区 | 91久久久久久久久久久久久 | 色人阁在线视频 | 欧美精品网址 | 91看片网页| 中文字幕综合在线观看 | av色偷偷| 国产精品久久久免费观看 | 久久精品无码一区二区日韩av | 国产一区二区三区四区波多野结衣 | 日韩在线欧美在线 | 国产午夜探花 | 日本一区二区三区四区高清视频 | 99久久久精品国产一区二区 | 一级黄片毛片免费看 | 国产精品刺激对白麻豆99 | 黄色网址免费在线播放 | 手机在线看片国产 | 黄色毛片免费看 | 国产精品高潮99久久久久久久 | asian超清日本肉体pics | 污版视频在线观看 | 久色亚洲 |