英文:
How to Reload Images that are not load, on Next.js
问题
以下是您提供的内容的翻译:
"Hello,我有一些IPFS数据,其中包含300张图片。我想要渲染它们,但是当我尝试渲染它们时,有时候其中3到4张图片无法加载。它会给我404错误,无法加载。所以,即使我重新加载页面,那些无法加载的图片仍然不会加载。它们似乎是永久无法加载的,我不知道该怎么办。
我尝试了在<Image/>中进行一些错误处理,比如设置状态等,但没有起作用。
这是我的代码。"
英文:
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 'next/link';
import React, { useEffect, useCallback, useState } from 'react';
import { NextPage } from 'next';
import Image from 'next/image';
import style from '../styles/pagesStyles/darwinswap1.module.scss';
import InfiniteScroll from 'react-infinite-scroll-component';
const DarwinSwap1: NextPage = () => {
const [ipfsData, setIpfsData] = useState([]);
const [items, setItems] = useState([]);
const fetchMoreData = () => {
setItems((prevItems) => [...prevItems, ...ipfsData.slice(prevItems.length, prevItems.length + 20)]);
if (items.length === 300) {
}
};
useEffect(() => {
const imageUrls = [];
for (let i = 1; i <= 100; i++) {
imageUrls.push(
`https://ipfs.filebase.io/ipfs/bafybeiczmjtyrtomsynlge2esqmdde2vmhkjc2ydv5kbq2lqwg2oobeqeq/${i}.png`
);
}
for (let i = 1001; i <= 1100; i++) {
imageUrls.push(
`https://ipfs.filebase.io/ipfs/bafybeiczmjtyrtomsynlge2esqmdde2vmhkjc2ydv5kbq2lqwg2oobeqeq/${i}.png`
);
}
for (let i = 2001; i <= 2100; i++) {
imageUrls.push(
`https://ipfs.filebase.io/ipfs/bafybeiczmjtyrtomsynlge2esqmdde2vmhkjc2ydv5kbq2lqwg2oobeqeq/${i}.png`
);
}
setIpfsData(imageUrls);
setItems(imageUrls.slice(0, 20));
}, []);
console.log(items);
return (
<div>
<InfiniteScroll
dataLength={items.length}
next={fetchMoreData}
hasMore={items.length === 300 ? false : true}
loader={<p style={{ textAlign: 'center', fontWeight: 600, opacity: 0.8, fontSize: '20px' }}>Scroll ⬇️</p>}
endMessage={
<p style={{ textAlign: 'center', fontWeight: 600, opacity: 0.8, fontSize: '20px' }}>No more data to load.</p>
}
>
<div className={style.container}>
{items.map((item, i) => {
return (
<div className={style.nfts} key={i}>
<Image layout="fill" src={item} alt={item} />
</div>
);
})}
</div>
</InfiniteScroll>
<div className={style.bottomContainer}>
<div className={style.description}>
Evoture NFTs are available through participation in our pre-sale or by buying them direct on OpenSeas!
</div>
<div className={style.buttonsContainer}>
<div className={style.presaleButtonContainer}>
<Link href="" className={style.presale}>
Join the Private Sale
</Link>
</div>
<div className={style.buyNft}>Buy NFT</div>
</div>
</div>
</div>
);
};
export default DarwinSwap1;
答案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: ["ipfs.filebase.io"],
},
}
Reference: https://nextjs.org/docs/pages/api-reference/components/image#domains
答案2
得分: 1
<img onerror="dosomething()" ...>
英文:
<img onerror="dosomthing()" ...>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论