Blank screen when lazy loading many images in NextJS tsx + Tailwindcss

I am displaying a series of images hosted in CDN on my NextJS page in the “masonry layout” structure (Just like Pinterest). As shown in .

When I try to load it on my desktop it’s working fine. But only when I load it on my iPhone or lower screen sizes, the screen

or it displays “A problem repeatedly occurred”() Can you please tell me how to fix it or debug it.

The code is as below for this page. All of this happens inside a [slug] folder for multi page rendering.

'use client'
import React, { useEffect, useState } from 'react'
import Image from 'next/image'
import { useParams, useRouter } from 'next/navigation'
import { IoDownloadOutline } from "react-icons/io5";
import { useInView } from 'react-intersection-observer';
import LoadingSpinner from '../../components/LoadingSpinner'
import { RiGeminiFill } from "react-icons/ri";

interface Wallpaper {
  id: string | number
  thumbnail_url: string
  downloads: number
  categories: Array<string>
  resolution_count: number
}

function CategoryPage() {
  const [wallpapers, setWallpapers] = useState<Wallpaper[]>([])
  const [page, setPage] = useState(1)
  const [hasMore, setHasMore] = useState(true)
  const { ref, inView } = useInView()
  const params = useParams()
  const router = useRouter()
  const [initialLoading, setInitialLoading] = useState(true)

  useEffect(() => {
    const fetchWallpapers = async () => {
      try {
        const response = await fetch(`${process.env.BACKEND_PUBLIC_API_URL}/fetch_wallpapers/category/${params.category}?page=${page}`)
        const data = await response.json()
        
        if (data.length === 0) {
          setHasMore(false)
          return
        }

        setWallpapers(prev => {
          const existingIds = new Set(prev.map((w: Wallpaper) => w.id))
          const newWallpapers = data.filter((w: Wallpaper) => !existingIds.has(w.id))
          return [...prev, ...newWallpapers]
        })
      } catch (error) {
        console.error('Error fetching wallpapers:', error)
      } finally {
        setInitialLoading(false)
        setIsLoading(false)
      }
    }

    if (hasMore) {
      fetchWallpapers()
    }
  }, [params.category, page, hasMore])

  const [isLoading, setIsLoading] = useState(false)

  useEffect(() => {
    if (inView && hasMore && !isLoading) {
      setIsLoading(true)
      setPage(prev => prev + 1)
    }
  }, [inView, hasMore, isLoading])
  
  useEffect(() => {
    setWallpapers([])
    setPage(1)
    setHasMore(true)
    setIsLoading(false)
  }, [params.category])

  const handleWallpaperClick = (wallpaper: Wallpaper) => {
    router.push(`/wallzone/wallpaper_view/${wallpaper.id}?categories=${wallpaper.categories.join(',')}`)
  }

  return (
    <div className="p-4 bg-paper-white min-h-screen pt-20
    max-sm:p-1 max-sm:pt-20">
      {initialLoading ? (
        <div className="h-[calc(100vh-80px)] flex items-center">
          <LoadingSpinner />
        </div>
      ) : (
        <div className="gap-3
        max-sm:columns-2 max-sm:gap-1 
        md:columns-3 
        lg:columns-5 
        2xl:columns-6 2xl:gap-4 [column-fill:_balance]">
          {wallpapers.map((wallpaper) => (
            <div key={wallpaper.id} className="break-inside-avoid mb-4" onContextMenu={(e) => e.preventDefault()}>
              <div 
                className="relative w-full overflow-hidden rounded-lg group cursor-pointer" 
                onClick={() => handleWallpaperClick(wallpaper)}
              >
                {wallpaper.thumbnail_url && (<Image
                  src={wallpaper.thumbnail_url}
                  alt={wallpaper.id.toString()}
                  width={0}
                  height={0}
                  loading="lazy"
                  className="w-full h-auto lg:hover:scale-105 lg:transition-transform lg:duration-300"
                  sizes="(max-width: 640px) 90vw,
                         (max-width: 768px) 45vw,
                         (max-width: 1024px) 30vw,
                         22vw"
                  style={{
                    width: '100%',
                    height: 'auto',
                    minHeight: '100px',
                  }}
                  unoptimized
                />)}
                {wallpaper.resolution_count === 4 && (
                  <div className="absolute top-2 right-2">
                    <RiGeminiFill className="text-yellow-400 text-xl" />
                  </div>
                )}
                <button className="absolute flex items-center rounded-full px-2 shadow-md text-white opacity-0 group-hover:opacity-100 transition-opacity duration-300
                max-lg:bottom-2 max-lg:right-2 
                lg:bottom-1 lg:right-1">
                  <IoDownloadOutline className="mr-1" />
                  {wallpaper.downloads}
                </button>
              </div>
            </div>
          ))}
        </div>
      )}
      {isLoading && !initialLoading && (
        <div className="py-4">
          <LoadingSpinner />
        </div>
      )}
    </div>
  )
}

export default CategoryPage

I think you need to change a bit in the Image component call.

change like this

  {wallpaper.thumbnail_url && (<Image
                  src={wallpaper.thumbnail_url}
                  alt={wallpaper.id.toString()}
                  width={0}
                  height={0}
                  loading="lazy"
                  className="w-full h-auto lg:hover:scale-105 lg:transition-transform lg:duration-300"
                  sizes="(max-width: 640px) 90vw,
                         (max-width: 768px) 45vw,
                         (max-width: 1024px) 30vw,
                         22vw"
                  style={{
                    width: '100%',
                    height: 'auto',
                    minHeight: '100px',
                  }}
                  unoptimized
                />)}

and you can also add the below code on your next.config.js

images: {
    deviceSizes: [640, 750, 828, 1080, 1200, 1920], // for different size of devices
    imageSizes: [16, 32, 48, 64, 96, 128, 256, 384],
  },
enter code here

Hope this helps.

New contributor

Ajoy_Sarker is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật