如何在Next.js中重新加载未加载的图像。

huangapple go评论82阅读模式
英文:

How to Reload Images that are not load, on Next.js

问题

以下是您提供的内容的翻译:

"Hello,我有一些IPFS数据,其中包含300张图片。我想要渲染它们,但是当我尝试渲染它们时,有时候其中3到4张图片无法加载。它会给我404错误,无法加载。所以,即使我重新加载页面,那些无法加载的图片仍然不会加载。它们似乎是永久无法加载的,我不知道该怎么办。

我尝试了在<Image/>中进行一些错误处理,比如设置状态等,但没有起作用。

这是我的代码。"

英文:

如何在Next.js中重新加载未加载的图像。Hello I have some ipfs data and it has 300 images. I want to render them but when I try to render them sometimes 3 4 of them is not loaded. it gives me 404 error and won't load. So even if i reload page the ones with not loaded is still not load. They are perma not loaded, i don't know what to do.

I tried some error handling in <Image/> like setting state etc. but didn't work.

This is my code.

import Link from &#39;next/link&#39;;
import React, { useEffect, useCallback, useState } from &#39;react&#39;;
import { NextPage } from &#39;next&#39;;
import Image from &#39;next/image&#39;;
import style from &#39;../styles/pagesStyles/darwinswap1.module.scss&#39;;
import InfiniteScroll from &#39;react-infinite-scroll-component&#39;;

const DarwinSwap1: NextPage = () =&gt; {
  const [ipfsData, setIpfsData] = useState([]);
  const [items, setItems] = useState([]);

  const fetchMoreData = () =&gt; {
    setItems((prevItems) =&gt; [...prevItems, ...ipfsData.slice(prevItems.length, prevItems.length + 20)]);
    if (items.length === 300) {
    }
  };

  useEffect(() =&gt; {
    const imageUrls = [];
    for (let i = 1; i &lt;= 100; i++) {
      imageUrls.push(
        `https://ipfs.filebase.io/ipfs/bafybeiczmjtyrtomsynlge2esqmdde2vmhkjc2ydv5kbq2lqwg2oobeqeq/${i}.png`
      );
    }
    for (let i = 1001; i &lt;= 1100; i++) {
      imageUrls.push(
        `https://ipfs.filebase.io/ipfs/bafybeiczmjtyrtomsynlge2esqmdde2vmhkjc2ydv5kbq2lqwg2oobeqeq/${i}.png`
      );
    }
    for (let i = 2001; i &lt;= 2100; i++) {
      imageUrls.push(
        `https://ipfs.filebase.io/ipfs/bafybeiczmjtyrtomsynlge2esqmdde2vmhkjc2ydv5kbq2lqwg2oobeqeq/${i}.png`
      );
    }
    setIpfsData(imageUrls);
    setItems(imageUrls.slice(0, 20));
  }, []);

  console.log(items);
  return (
    &lt;div&gt;
      &lt;InfiniteScroll
        dataLength={items.length}
        next={fetchMoreData}
        hasMore={items.length === 300 ? false : true}
        loader={&lt;p style={{ textAlign: &#39;center&#39;, fontWeight: 600, opacity: 0.8, fontSize: &#39;20px&#39; }}&gt;Scroll ⬇️&lt;/p&gt;}
        endMessage={
          &lt;p style={{ textAlign: &#39;center&#39;, fontWeight: 600, opacity: 0.8, fontSize: &#39;20px&#39; }}&gt;No more data to load.&lt;/p&gt;
        }
      &gt;
        &lt;div className={style.container}&gt;
          {items.map((item, i) =&gt; {
            return (
              &lt;div className={style.nfts} key={i}&gt;
                &lt;Image layout=&quot;fill&quot; src={item} alt={item} /&gt;
              &lt;/div&gt;
            );
          })}
        &lt;/div&gt;
      &lt;/InfiniteScroll&gt;
      &lt;div className={style.bottomContainer}&gt;
        &lt;div className={style.description}&gt;
          Evoture NFTs are available through participation in our pre-sale or by buying them direct on OpenSeas!
        &lt;/div&gt;
        &lt;div className={style.buttonsContainer}&gt;
          &lt;div className={style.presaleButtonContainer}&gt;
            &lt;Link href=&quot;&quot; className={style.presale}&gt;
              Join the Private Sale
            &lt;/Link&gt;
          &lt;/div&gt;
          &lt;div className={style.buyNft}&gt;Buy NFT&lt;/div&gt;
        &lt;/div&gt;
      &lt;/div&gt;
    &lt;/div&gt;
  );
};

export default DarwinSwap1;

如何在Next.js中重新加载未加载的图像。

答案1

得分: 1

You need to add ipfs.filebase.io domain in your next.config.js file:

module.exports = {
  images: {
    domains: ["ipfs.filebase.io"],
  },
}

Reference: https://nextjs.org/docs/pages/api-reference/components/image#domains

英文:

You need to add ipfs.filebase.io domain in your next.config.js file:

module.exports = {
  images: {
    domains: [&quot;ipfs.filebase.io&quot;],
  },
}

Reference: https://nextjs.org/docs/pages/api-reference/components/image#domains

答案2

得分: 1

<img onerror="dosomething()" ...>

英文:
&lt;img onerror=&quot;dosomthing()&quot; ...&gt;

huangapple
  • 本文由 发表于 2023年5月8日 00:34:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/76195092.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定