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
Buttons are displayed before the api loads inside the table and loader not working

I have a page where an api loads slowly and only two buttons are displayed in my page for some seconds so I have decided to add loader in that page I have tried the below code but the loader is loaded not the page table

import Loader from '../Loader/Loader';
const FileTable = () => {
    const [isVisible, setIsVisible] = useState(true);
        const handleLoading = () => {
         setIsVisible(false);
        }
    useEffect(() => {
        window.addEventListener("load",handleLoading);
        return () => window.removeEventListener("load",handleLoading)
    },[])
    return !isVisible ? (
           <div>
             My component
           </div>
    ):(
        <div>
           <h1>Loading...</h1>
        </div>
    )
}
export default FileTable;

I even tried the below code

const [isVisible, setIsVisible] = useState(false);

and then

if(!isVisible){
 return <>Loading</>
}

return(
<>
   My Component
</>
)

I have tried both the ways but the loader is not working. The buttons should also hide when the page loads only the loader should be displayed until the api loads How do I fix it?

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

0

A simple and straightforward scenario would be:

  • Initialize your component loading state to true
  • On component mount, initialize API call
  • After API call, set component loading state to false
  • Your component should, based on the loading state, render either a loader or the retrieved data from the API call

A sample code would be as the following:

const MyComponent = () => {
  const [loading, setLoading] = useState(true);
  const [apiData, setApiData] = useState([]); // or whatever
  const handleClose = () => {
      setLoading(false)
  }
  useEffect(() => {
      // Init API call here
      // then( ..... setApiData()) // Do whatever with API response to set component apiData
      // finally (() => setLoading(false))
  }, []) // empty dependency array is equivalent to componentDidMount

  if(loading) {
    return <Loader />
  }
  return <Component data={apiData} />
}

It is also a good practice to handle eventual API call failures

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