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

155
Vistas
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 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