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

159
Views
loading while fetching firestore data

While fetching some data from firestore, I am setting a loading state , but this state will return to true once again , I don't know why :

  const [isLoading, setIsLoading] = React.useState(true)
nprogress.start()
  const fetchGames=async()=>{
    const eventsFromDb = []
    const DB =await db.collection('event').get()
    DB.docs.forEach((item,index)=>{
      eventsFromDb[index]= item.data()
     })
     setEvents(eventsFromDb)
     setIsLoading(false)
     console.log(isLoading) // consoling this will return true after the progress is done(false)
    }
    
    useEffect(()=>
    {
      fetchGames()
      if(!isLoading)
      {
        nprogress.done()
      }

    }, [isLoading])

How can I fix this ? after it finished loading it starts over again

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

React sets its state asynchronously so right after setting the state can still be true

Try it like this

useEffect(() => {
  fetchGames()
}, [])

useEffect(() => {
  if (!isLoading) {
    nprogress.done()
  }
}, [isLoading])
about 4 years ago · Juan Pablo Isaza Report

0

I fixed this issue , by just ordering the code , since I was setting the Loading after I store the data in an array :

const fetchGames=async()=>{
    const eventsFromDb = []
    const DB =await db.collection('event').get()
    DB.docs.forEach((item,index)=>{
      eventsFromDb[index]= item.data()
     })
     setIsLoading(false) // This will be replaced above the setEvents
     setEvents(eventsFromDb)
   
     console.log(isLoading) // 
    }
about 4 years ago · Juan Pablo Isaza Report
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!