Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

257
Vistas
Infinite scroll with API call

I would like to make an infinite scroll with Intersection Observer that in the call back function increments the page number from an API and displays another image.

I am stuck on how I can call the API again and increment the page.

This is what I tried:

      const [page, setPage] = useState(1);
      const [images, setImages] = useState(null);
      const lastItem = useRef(null);

      useEffect(() => {
    const intersection = new IntersectionObserver(() => {
      // setPage(page + 1);
    });

    if (lastItem.current) {
      intersection.observe(lastItem.current);
    }
  

    });

      useEffect(() => {
        async function dataSet() {
          const data = await fetch(`https://api.themoviedb.org/3/trending/movie/week?api_key=${apiKey}&page=${page}`).then((res) => res.json());
          setImages(data);
        }
        dataSet();
      }, [apiKey, page]);

  useEffect(() => {});

  if (!images) {
    return <h2>Loading...</h2>;
  }

  return (
    <>
      <Nav />
      <main>
        <section className={styles.spacer}>
          <h1>Trending Movies</h1>
          <ul className={styles.grid}>
            {images.results?.map((image) => {
              return (
                <li className={styles.gridItem} key={image.id} ref={lastItem}>
                  <img src={`https://image.tmdb.org/t/p/w370_and_h556_bestv2/${image.poster_path}`} alt={image.name} />
                  <p className={styles.title}>{image.original_title}</p>
                </li>
              );
            })}
          </ul>
        </section>
      </main>
      <Footer />
    </>
  );
}
about 4 years ago · Juan Pablo Isaza
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda