Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

118
Views
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
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!