简体   繁体   English

单击图库中的最终图像时,幻灯片显示“图像未定义”

[英]Slideshow says ' image is undefined' when clicking next on the final image in the gallery

Have a slideshow with an index of images, I want it to close when you click the 'next' icon when its at the last image in the gallery.有一个带有图像索引的幻灯片,我希望它在您单击“下一个”图标时关闭,当它位于图库中的最后一张图像时。 So far I'm able to close the gallery if you click the previous arrow on the first image, but if you click on the next arrow on the final image, the page gets the error: TypeError: Cannot read properties of undefined (reading 'image') .到目前为止,如果您单击第一张图片上的上一个箭头,我可以关闭图库,但是如果您单击最后一张图片上的下一个箭头,页面会出现错误: TypeError: Cannot read properties of undefined (reading 'image') I assume its because it is trying to read an image that isn't there, and not something else as all of my images are showing fine.我认为它是因为它试图读取不存在的图像,而不是其他东西,因为我的所有图像都显示良好。 imagesData queries the data from the backend. imagesData从后端查询数据。

  const [slideIndex, setSlideIndex] = useState(0);
  const [openImage, setOpenImage] = useState(false);

  const handleOpenImage = (index) => {
      setSlideIndex(index)
      setOpenImage(true)
  }

  const handleCloseImage = () => {
      setOpenImage(false)
  }

  const slidePrev = () => {
      slideIndex === 0
      ? setSlideIndex( imagesData.length - 1)
      : setSlideIndex( slideIndex - 1)
      if(slideIndex <= 0){
        setOpenImage(false)
      }
  }

  const slideNext = () => {
      slideIndex + 1 === imagesData.length
      ? setSlideIndex(0)
      : setSlideIndex(slideIndex + 1)
      if(slideIndex > imagesData.length + 1){
        setOpenImage(false)
      }
  }

you should not use state value after calling the setSlideIndex callback function, because the callback function takes some time to be done so the next statement has access to the previous value在调用 setSlideIndex 回调 function 后,您不应使用 state 值,因为回调 function 需要一些时间才能完成,因此下一条语句可以访问上一个值

try the following code:试试下面的代码:

  const [slideIndex, setSlideIndex] = useState(0);
  const [openImage, setOpenImage] = useState(false);

  const handleOpenImage = (index) => {
      setSlideIndex(index)
      setOpenImage(true)
  }

  const handleCloseImage = () => {
      setOpenImage(false)
  }

  const slidePrev = () => {
      if(slideIndex === 0){
        setOpenImage(false)
      } else {
    setSlideIndex( slideIndex - 1)
    }
  }

  const slideNext = () => {
      if(slideIndex === imagesData.length-1 ){
        setOpenImage(false)
      } else {
      setSlideIndex(slideIndex + 1)
      }
  }
 you can write this logic :
 

    const slidePrev = () => {
          slideIndex === 0
          ? setOpenImage(false)
          : setSlideIndex( slideIndex - 1)
      }
    
      const slideNext = () => {
          setSlideIndex(slideIndex + 1)
          slideIndex <= imagesData.length
          ? setSlideIndex(slideIndex )
          : setOpenImage(false)
          
      }

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM