简体   繁体   English

从数组 React.js 中删除丢失的图像

[英]Remove missing Images from array React.js

I have array of images coming from external API, I want to filter images which are broken before showing them on carousel.我有来自外部 API 的图像数组,我想过滤损坏的图像,然后再将它们显示在轮播上。 as you can see now I map every image and showing MissingImage component as unloader if image is missing, but in my situation sometimes there are more than half images not available on API.正如你现在所看到的,我 map 每个图像并在图像丢失时将 MissingImage 组件显示为卸载程序,但在我的情况下,有时 API 上没有一半以上的图像。 how can I skip images if they aren't available before passing them to Carousel component?如果图像在传递给 Carousel 组件之前不可用,我该如何跳过它们?

          {images.map((img, i) => (
        <Dot
          key={i}
          type="button"
          onClick={() => {
            setThumbsPosition(i);
            goToSlide(i);
            setActiveSlide(i);
          }}
          active={activeSlide === i}
          ref={i === 0 && slideRef}
        >
          <SliderDotImage>
            <Img
              loading="lazy"
              src={[img.url.bigger]}
              unloader={<MissingImage small />}
            />
          </SliderDotImage>
        </Dot>
      ))}

You can use && operator to check whether we have an image URL or not:您可以使用 && 运算符来检查我们是否有图像 URL :

{images.map((img, i) => (
        <Dot
          key={i}
          type="button"
          onClick={() => {
            setThumbsPosition(i);
            goToSlide(i);
            setActiveSlide(i);
          }}
          active={activeSlide === i}
          ref={i === 0 && slideRef}
        >
          <SliderDotImage>
            <Img
              loading="lazy"
              src={img?.url && img.url} // or src={img && img.url}
              unloader={<MissingImage small />}
            />
          </SliderDotImage>
        </Dot>
      ))}

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

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