在使用useEffect、useCallback等hook时,为什么要写依赖数组?如果你不写这些依赖,React会给你一个警告。下面分享一下我对声明依赖目的的理解:防止函数的闭包特性导致的意外错误。目的是想让它每1秒跳转到下一盏灯。Red=>Green=>Yellow=>Red=>Green=>Yellow但是我发现下面这串代码只有绿灯和黄灯亮。importReact,{useState,useEffect}from"react";importclassesfrom"./TrafficLight.module.css";importLightfrom"./Light";letcurIdex=0;constTrafficLight=()=>{const[灯,setLights]=useState([{on:true,color:"red",id:0},{on:false,color:"green",id:1},{on:false,color:"yellow",id:2},]);console.log('当前灯:',灯);useEffect(()=>{letcurIdex=0;consttimer=setInterval(()=>{curIdex+=1;if(curIdex>=lights.length)curIdex=0;console.log('receivedlights',lights)toggleLight(lights[curIdex]);},1000*1);},[]);返回()=>清除超时(计时器);},[灯]);consttoggleLight=(curLight)=>{setLights(lights.map((light)=>light===curLight?{...light,on:!light.on}:{...light,on:false}));};常量htList=lights.map((light)=>{return
