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

119
Vistas
Adding infinite scroll to my React by fetching data from firestore

I am fetching crypto trades data from my collection in firestore. On clicking the load more button, I am successfully loading more data hence the query is not the issue here. I want to run the function when the user scrolls to the bottom. Here is my query:

const tradesRef = firebase
    .firestore()
    .collection("cex-trades")
    .orderBy("time", "desc")
    .limit(16);

Here is the initial data loading when the component is mounted:

 useEffect(() => {
    tradesRef.get().then((collections) => {
      const tradesData = collections.docs.map((trade) => trade.data());
      const lastDoc = collections.docs[collections.docs.length - 1];
      setTrades(tradesData);
      setLastTrades(lastDoc);
    });
    setDataLoading(false);
  }, []);

When the page loads, the first 16 trades are loaded on the screen. I have a Fetch More button which loads the next 16 trades and so on. The fetch more function:

const fetchMore = () => {
    tradesRef
      .startAfter(lastTrades)
      .get()
      .then((collections) => {
        const tradesData = collections.docs.map((trade) => trade.data());
        const lastDoc = collections.docs[collections.docs.length - 1];
        setTrades((listOfTrades) => [...listOfTrades, ...trades]);
        setLastTrades(lastDoc);
      });
  };

I do not want to make the users click the button to load the next trades. The next trades should be loaded when the user reaches the end of the page by scrolling. How can I achieve this through React?

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