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

154
Visualizações
Are changes to React component state always executed in call order?

I have an app that fetches data from the API. There is no safe default value that I can assign to a piece of state, because the API may return any value. As a solution, I want to set an element of state to true after completing the API call.

const [data, setData] = useState<any>()
const [dataLoaded, setDataLoaded] = useState(false)
useEffect(() => {
  setDataLoaded(false)
  Promise.resolve("myData").then(res => {
    setData(res)
    setDataLoaded(true)
  })

  }
}, [])

Will the following code, is it guaranteed that dataLoaded will be set to true only after data is populated? I understand that I can't, for example, guarantee myself access the contents of data in the call to setDataLoaded, but does that mean that the state changes themselves can execute in any order?

If not, how can I guarantee that it will be?'

I know that this guarantee does exist in class components, but I don't know if it does in hooks, especially if it's happening inside a promise.

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