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

135
Vistas
How can i add loading spinner before fetching data in react?

What is the correct way to add a spinner while fetching data from API in react js? I tried to display the spinner until the data.length > 0 it works fine when the data is more than zero. But when it is actually zero, the loading spinner doesn't stop. Is there any good solution for this?

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

You didn't provide enough info or code to tell exactly what to do but i will give you a simple example

just create a state for the loading set it true and when the fetch is done set it false

I hope this example is clear

import { useEffect,useState } from 'react'

export default function Page() {
    const [data, setData] = useState(null)
    const [loading, setLoading] = useState(true)

    useEffect(() => {
        fetch('/api/test',{method: 'GET'})
        .then(res => res.json())
        .then(data => {
            console.log(data)
            setData(data)
        }
        )
        .catch(err => {
            console.log(err)
        }
        )
        .finally(() => {
            setLoading(false)
        })
    }, [])
    if(loading) return <div>Loading...</div> // or your loading spinner
    return (
    <div>{JSON.stringify(data)}</div>
  )
}

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