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

193
Vistas
the function doSomethingWithData(v) is being called with the wrong value of the variable V. T
    const Hello = () => {
      const [v, setV] = React.useState(1);
    
      React.useEffect(() => {
        setTimeout(async () => {
          await setV(2);
          doSomethingWithData(v)
        }, 3000)
      }, []);
    
    
      return (
        <div>
          <h1>Hello, world {v}!</h1>
        </div>
      );
    }
    
    const doSomethingWithData = (v) => {
      //report e-commerce data to our server here..
      
    console.log('Variable value is: ' + v);
    }
    
    ReactDOM.render(
      <Hello />,
      document.getElementById('root')
    );

the function doSomethingWithData(v) is being called with the wrong value of the variable V. That is problematic because a record will be created in our e-commerce database when doSomethingWithData() is called and as a result we might make choices based on faulty data.can someone explain this?

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

The following does not work as you expect. setV is async but it does not return a promise to wait for.

async () => {
    await setV(2);
    doSomethingWithData(v);
};

The general pattern is to use useEffect hook with a dependency value which is v in your case.

React.useEffect(() => {
    setTimeout(() => {
        setV(2);
    }, 3000);
}, []);

React.useEffect(() => {
    doSomethingWithData(v);
}, [v]);
about 4 years ago · Juan Pablo Isaza Denunciar

0

If you have the data available just pass that to doSomethingWithData.

    React.useEffect(() => {
      setV(2);
      doSomethingWithData(2)
  }, []);

The view will display the data from the state v and doSomethingWithData won't be a state behind.

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