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

96
Vistas
How to avoid inconsistencies in dependent React state when updating based on promises

In the following example, if the "slow" button is clicked immediately followed by a click of the "fast" button, dependsOnA will first be set to "Result from fast promise" since the fast promise resolves first. After about a second the slow promise will resolve and dependsOnA will be set to "Result from slow promise" . What I really want is for the state dependsOnA to always reflect the current state of a. I.e. dependsOnA should always get the value "Result from fast promise" when a is set to true and "Result from slow promise" when a set to false.

What is the best way of accomplishing this?

import * as React from 'react';

const slowPromise: () => Promise<string> = () => {
    return new Promise((resolve) => {
        setTimeout(() => resolve('Result from slow promise'), 2000);
    });
};

const fastPromise: () => Promise<string> = () => {
    return new Promise((resolve) => {
        setTimeout(() => resolve('Result from fast promise'), 1000);
    });
};

export function Example(props: {}) {
    const [a, setA] = React.useState<boolean>();
    const [dependsOnA, setDependsOnA] = React.useState<string>();

    React.useEffect(() => {
        if (a)
            fastPromise().then(setDependsOnA);
        else
            slowPromise().then(setDependsOnA);
    }, [a]);

    return (
        <div>
            <button onClick={() => setA(false)}>Slow</button>
            <button onClick={() => setA(true)}>Fast</button>
            {dependsOnA}
        </div>
    );
}

I came up with the following solution which uses the cleanup callback of the useEffect hook to "cancel" the state update after the promise has resolved if a has changed since the promise was created. However, this solution feels a bit hacky.

    React.useEffect(() => {
        let isCanceled: boolean = false;
        if (a)
            fastPromise().then((res) => {
                if (!isCanceled) setDependsOnA(res);
            });
        else
            slowPromise().then((res) => {
                if (!isCanceled) setDependsOnA(res);
            });
        return () => { isCanceled = true; };
    }, [a]);
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