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

142
Visualizações
Does calling useState function inside another useState callback cause multiple renders?

I have some code like the sample below where I call a state setter inside the callback for another state setter. All of this needs to happen inside a useCallback hook to avoid the useEffect hook being called on every render.

My question is, how many renders does the useCallback hook trigger? Would setBar here trigger another render? And if so, is there a more efficient way to write this to trigger fewer renders (without removing the useEffect hook)?

Bear in mind that the code below is just a simplified example of the code in my application.

const [foo, setFoo] = useState(0)
const [bar, setBar] = useState(0)

const baz = useCallback(() => {
  setFoo((prevState) => {
    const newState = prevState += 1
    // does this trigger another render?
    setBar(newState)
    return newState
  })
})

useEffect(() => baz, [baz])

about 4 years ago · Juan Pablo Isaza
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