Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

104
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda