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

102
Vistas
can someone explaine how setstate work inside useffect

i find a code that show loading before fetching data and shows "downloaded " after the fetching is ended the code is working ,however i couldn't understand how this code is working for my understanding the useEffect() will run one time when the app start and usestate() will rerender the component every time loading value changed so ,in the beginning of the code the useeffect() will run ,and setloading willchange the value to true and rerender the component ,for my point of vue it look like there is infinite loop of rendering can someone please explaine how usestate worke inside of useffect and how this code is working. this is the code:

import { useState } from "react";
import { useEffect } from "react";
export default function Assemble(){
const [loading,setloading]=useState(true);
useEffect(()=>{
   setloading(true);
      fetch('https://jsonplaceholder.typicode.com/photos')
  .then(response => response.json())
  .then(json => console.log(json))
  setloading(false);
},[])
   return(<div>
      <div>{loading?"Loading":"downloaded"}</div>
   </div>)
} ```

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

0

From my knowledge, the useEffect can get 2 arguments as parameters, 1 of them being the function that should be run, and the second one being an array (in your case it was []) which mentions upon the change of which dependencies should the first function be executed.

Now, because you used [] and left it empty, that means the useEffect will only run once.

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