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

156
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
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