简体   繁体   English

修复未捕获的类型错误:无法读取未定义的属性(读取“样式”)

[英]Fixing Uncaught TypeError: Cannot read properties of undefined (reading 'style')

I'm trying to make a slideshow in my react app.我正在尝试在我的 React 应用程序中制作幻灯片。 I'm using the below function for the slideshow.我正在使用下面的 function 进行幻灯片放映。

 var myIndex = 0; carousel(); function carousel() { var i; var x = document.getElementsByClassName("diabetes-day"); for (i = 0; i < x.length; i++) { x[i].style.display = "none"; } myIndex++; if (myIndex > x.length) { myIndex = 1 } x[myIndex - 1].style.display = "block"; setTimeout(carousel, 5000); // Change image every 5 seconds }
 <div className="slideshow-container"> <div className="diabetes-news"> <img className="diabetes-day" src={DiabetesDay}/> <img className="diabetes-day" src={Symptoms}/> <img className="diabetes-day" src={Managing}/> </div> </div>

When I run the code the slideshow works.当我运行代码时,幻灯片可以正常工作。 But as soon as I refresh the page all the contents in the page disappears and I get the following error in my console.但是,一旦我刷新页面,页面中的所有内容就会消失,并且我的控制台中出现以下错误。 错误截图

I'm not quite sure why it stops working once the page is refreshed.我不太确定为什么刷新页面后它会停止工作。 It would be great if someone could guide me on how to fix this.如果有人可以指导我如何解决此问题,那就太好了。

I have also encountered the same error on refreshing the page.我在刷新页面时也遇到了同样的错误。 I have used the useEffect hook to solve this error.我已经使用 useEffect 钩子来解决这个错误。

useEffect(() => {
  //your code
}, [value that is changing]);

it seems that you are working with reactjs, it's better to change your code with this:看起来你正在使用 reactjs,最好用这个来改变你的代码:

 const {useState, useEffect} = React; const Example = () => { const [imageIndex, setImageIndex] = useState(0); // change these images with yours const imagesSrc = [ "https://cdn.pixabay.com/photo/2022/03/03/11/19/nature-7045133_960_720.jpg", "https://cdn.pixabay.com/photo/2015/04/23/22/00/tree-736885_960_720.jpg", "https://cdn.pixabay.com/photo/2013/07/18/20/26/sea-164989_960_720.jpg", ]; const carousel = () => { setImageIndex((prev) => (prev < 2? prev + 1: 0)); }; useEffect(() => { const timer = setInterval(() => { carousel(); }, 2000); // set the which timer you want return () => { clearInterval(timer); }; }, []); return ( <div className="slideshow-container"> <div className="diabetes-news"> <img className="diabetes-day" src={imagesSrc[imageIndex]} alt="" /> </div> </div> ); }; // Render it ReactDOM.render( <Example/>, document.getElementById("react") );
 .diabetes-day{ width: 200px; }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/react/17.0.1/umd/react.production.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/17.0.1/umd/react-dom.production.min.js"></script> <div id="react"></div>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 JavaScript(React)- 未捕获的类型错误:无法读取未定义的属性(读取“样式”) - JavaScript(React)- Uncaught TypeError: Cannot read properties of undefined (reading 'style') 未捕获的类型错误:无法读取未定义的属性(读取“8”) - Uncaught TypeError: Cannot read properties of undefined (reading '8') 未捕获的类型错误:无法读取未定义的属性(读取“0”) - Uncaught TypeError: Cannot read properties of undefined (reading '0') 未捕获的类型错误:无法读取未定义的属性(读取“0”) - Uncaught TypeError: Cannot read properties of undefined (reading '0') 未捕获的类型错误:无法读取未定义的属性(读取“”) - Uncaught TypeError: Cannot read properties of undefined (reading '') 类型错误:无法读取未定义的属性(读取“样式”) - TypeError: Cannot read properties of undefined (reading 'style') 未捕获的类型错误:无法读取未定义的属性(读取“样式”)style.width=&quot;200px&quot; - Uncaught TypeError: Cannot read properties of undefined (reading 'style') style.width="200px" 未捕获的类型错误:无法读取 null 的属性(正在读取“切片”)------ 未捕获的类型错误:无法读取未定义的属性(正在读取“过滤器”) - Uncaught TypeError: Cannot read properties of null (reading 'slice') ------ Uncaught TypeError: Cannot read properties of undefined (reading 'filter') Uncaught TypeError TypeError:无法读取未定义的属性(读取“路径”) - Uncaught TypeError TypeError: Cannot read properties of undefined (reading 'path') 未捕获的类型错误:无法读取未定义的属性(读取“目标”)和(读取“值”) - Uncaught TypeError: Cannot read properties of undefined (reading 'target') & (reading 'value')
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM