網(wǎng)站首頁(yè) 編程語(yǔ)言 正文
React中的for循環(huán)
記得要綁定key!
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<script src="./js/react.development.js"></script>
<script src="./js/react-dom.development.js"></script>
<script src="./js/babel.min.js"></script>
<title>例子2</title>
</head>
<body>
<div id="root1"></div>
<div id="root2"></div>
<div id="root3"></div>
</body>
<script type="text/babel">
//繼承實(shí)例
window.onload = () => {
var arr = ["a", "b", "d", "e", "f"];
//第一種寫法 >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
ReactDOM.render(
<div>
{
arr.map((item, index) => {
return <div key={index}>{item}</div>
})
}
</div>,
document.getElementById("root1")
)
//第二種寫法 >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
var str = arr.map((item, index) => {
return <div key={index}>{item}</div>
})
ReactDOM.render(
<div>
{str}
</div>,
document.getElementById("root2")
)
//第三種寫法 我們應(yīng)該是最熟悉這種寫法
var str=[];
for(let i=0;i<arr.length;i++){
str.push(<div key={i}>{arr[i]}</div>)
}
ReactDOM.render(
str,
document.getElementById("root3")
)
}
</script>
</html>
React死循環(huán)
原因1
修改狀態(tài)函數(shù)寫在副作用函數(shù)里面,修改狀態(tài)函數(shù)會(huì)使整個(gè)函數(shù)式組件重新執(zhí)行,相當(dāng)于執(zhí)行了以下代碼
export default function App () {
? const [num, setNum] = useState(5)
? console.log(setNum)
? document.title = '標(biāo)題' + num
? useEffect(() => {
? ? // setNum(num + 5)
? ? document.title = '標(biāo)題' + num
? })
? const hClick = () => {
? ? setNum(num + 5)
? ? // useEffect(() => {
? ? // ? // setNum(num + 5)
? ? // ? document.title = '標(biāo)題' + num
? ? // })
? ? // 錯(cuò)誤×
? ? // Error: Invalid hook call. Hooks can only be called inside of the body of a function component. This could happen for one of the following reasons:
? ? // 1. You might have mismatching versions of React and the renderer (such as React DOM)
? ? // 2. You might be breaking the Rules of Hooks
? ? // 3. You might have more than one copy of React in the same app
? ? // See https://reactjs.org/link/invalid-hook-call for tips about how to debug and fix this problem.
? }
? return (<div>
? ? ? num:{num}
? ? ? <button type="button" onClick={() => {
? ? ? ? // eslint-disable-next-line no-unused-expressions
? ? ? ? hClick()
? ? ? }}>每次加5</button>
? ? </div>)
}
錯(cuò)誤代碼如下:
? useEffect(() => {
? ? // setNum(num + 5)
? ? document.title = '標(biāo)題' + num
? })
總結(jié)
原文鏈接:https://yafei.blog.csdn.net/article/details/91359381
相關(guān)推薦
- 2022-06-21 C#多線程異步執(zhí)行和跨線程訪問(wèn)控件Helper_C#教程
- 2022-12-01 Entity?Framework使用DBContext實(shí)現(xiàn)增刪改查_實(shí)用技巧
- 2023-01-21 flask中響應(yīng)錯(cuò)誤的處理及errorhandler的應(yīng)用方式_python
- 2022-05-24 python使用torch隨機(jī)初始化參數(shù)_python
- 2022-06-25 python多進(jìn)程和多線程介紹_python
- 2022-07-12 springboot整合jasypt加密yml配置文件
- 2022-08-11 如何利用python繪制等高線圖_python
- 2023-01-02 Python利用socket實(shí)現(xiàn)多進(jìn)程的端口掃描器_python
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲(chǔ)小
- 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)證過(guò)濾器
- 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)-簡(jiǎn)單動(dòng)態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對(duì)象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支