Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

258
Visualizações
How to eager load an list or array of objects in NextJS?

I have the following next/image in one of my pages, with priority set to true to eager load the asset from the state

const [imageToUse, setImageToUse] = useState(initialImage);

<Image src={imageToUse} alt='scrolls' priority={true} ... />

I have set state in such a way that it would listen on scroll event and have it calls the state imageToUse from a new image as the user scrolls:

 const cycleImages = {
    1: "/header/_0011.webp",
    2: "/header/_0012.webp",
    3: "/header/_0013.webp",
    ....
    37: "/header/_0047.webp",
}

 useEffect(() => {
    // scrolled at every 30 pixels
    const y = clientWindowHeight / 30;
    // Scroll event distance + cycle through a list of objects
    let label = Math.min(Math.floor(y) + 1, 37)
    // Set imageToUse
    let image = cycleImages[label]
    setImageToUse(image)
}

The above logic works, but the only problem I have is that <Image /> only eager loads the initial image or the image that is about to be scrolled through - whereas I want it to eager load all 37 images and to avoid fetching ONLY when it is scrolls to and create stutter (i.e. bad user experience).

So how do I eager load all 37 images in NextJS?

about 4 years ago · Juan Pablo Isaza
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda