日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學(xué)無(wú)先后,達(dá)者為師

網(wǎng)站首頁(yè) 編程語(yǔ)言 正文

React中的for循環(huán)解讀_React

作者:越來(lái)越好。 ? 更新時(shí)間: 2023-03-11 編程語(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

欄目分類
最近更新