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

163
Visualizações
setting the state inside the useEffect with that state as a dependency

Let's say I have this code here:

const [a , setA] = useState(false);
useEffect(() => {
      setA(true);

    setTimeout(()=>{
      setA(false);
    },300);

},[a]);

I have two questions:

1) Will this create an infinte loop (if I'm using that same state as a dependency)?

2) If I set the state in the middle of the useEffect code the component re-render immediately, so when will the rest of the code execute if the component render before we get to it inside the useEffect? When will the setA(false) happen?

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

If you fix the syntax (setTimeOut needs to be spelled properly), yes, that'll cause an infinite loop.

const App = () => {
    console.log('rendering');
    const [a , setA] = React.useState(false);
    React.useEffect(() => {
          setA(true);

        setTimeout(()=>{
          setA(false);
        },300);

    },[a]);
    return 'foo';
};

ReactDOM.render(<App />, document.querySelector('.react'));
<script crossorigin src="https://unpkg.com/react@16/umd/react.development.js"></script>
<script crossorigin src="https://unpkg.com/react-dom@16/umd/react-dom.development.js"></script>
<div class='react'></div>

If I set the state in the middle of the useEffect code the component re-render immediately, so when will the rest of the code execute if the component render before we get to it inside the useEffect? When will the setA(false) happen?

It'll occur just as it was originally scheduled to - 300-ish milliseconds after the hook originally runs. When the component re-mounts, it will not cancel or change ongoing timeouts that previous renders might have created, unless you've specifically written code to handle that possibility.

  1. The component mounts or has setA called
  2. The component (re-)renders
  3. The effect hook runs
  4. If the effect hook eventually calls setTimeout, the timeout callback will run regardless, exactly at the time scheduled, no matter what other code there is - as long as you don't call clearTimeout. If you set state as well as calling setTimeout, that has no effect on the timeout. (which is an important thing to keep in mind - stale closures are a common problem people run into with React)
about 4 years ago · Juan Pablo Isaza Relatório

0

If I set the state in the middle of the useEffect code the component re-render immediately, so when will the rest of the code execute if the component render before we get to it inside the useEffect? When will the setA(false) happen?

No, It will not re-render the component in middle of useEffect even if you set the state. useState hook runs asynchronously. It will be queued.

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