網(wǎng)站首頁 編程語言 正文
列表渲染:
1.使用數(shù)組的 map 方法(因?yàn)閙ap 可以映射)
2、列表要添加 key 屬性,值要唯一
// 導(dǎo)入
// 用來獲取 dom元素
import { createRoot } from "react-dom/client";
// 內(nèi)容
const contentArr = [
{ id: 1, name: "唐" },
{ id: 2, name: "宋" },
{ id: 3, name: "元" },
{ id: 4, name: "明" },
{ id: 5, name: "清" },
];
// 標(biāo)識
const mark = 0;
const root = createRoot(document.querySelector("#root"));
root.render(
<div>
<ul>
{contentArr.map((item) => {
return <li key={item.id}>{item.name}</li>;
})}
</ul>
</div>
);
不加 key 值會報(bào)錯(cuò)
條件渲染
1、邏輯與 && --場景:當(dāng)只有一個(gè)內(nèi)容時(shí)使用,渲染 or 不渲染
2、三元表達(dá)式 ?: --場景: 有兩個(gè)內(nèi)容時(shí),渲染A 或者渲染B
樣式的處理
className + 樣式文件
// 標(biāo)識
const mark = 3;
const root = createRoot(document.querySelector("#root"));
root.render(
<div>
<ul>
{contentArr.map((item, index) => {
return (
<li
key={item.id}
className={item.id === mark ? "text" : "initialize"}
>
{index === 0 && (
<img
className="img-box"
src="https://p3-passport.byteimg.com/img/user-avatar/35e9831939c32731d1f9a2c2ff23a2ea~100x100.awebp"
alt=""
/>
)}
{item.name}
</li>
);
})}
</ul>
</div>
);
原文鏈接:https://blog.csdn.net/wbskb/article/details/131866328
- 上一篇:沒有了
- 下一篇:沒有了
相關(guān)推薦
- 2022-04-30 Matlab繪制散點(diǎn)密度圖的教程詳解_C 語言
- 2022-12-31 解決React報(bào)錯(cuò)`value`?prop?on?`input`?should?not?be?nul
- 2022-07-30 C++簡明圖解分析淺拷貝與深拷貝_C 語言
- 2022-04-21 catalina.out 和 catalina.log 的區(qū)別和用途
- 2023-05-20 openGauss數(shù)據(jù)庫共享存儲特性概述_數(shù)據(jù)庫其它
- 2022-12-27 python中g(shù)etopt()函數(shù)用法詳解_python
- 2023-10-27 np.array()函數(shù)的使用方法_python
- 2022-08-30 ST-Link設(shè)備連接。 Could not verify ST device! Abort con
- 欄目分類
-
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支