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

167
Vistas
set value to state in React

I am new to React, and I have a question of set state, this is the code:

this.setState((prevState) => {
                return{
                    prevVal: prevState.currentVal,
                    currentVal: 'DIGIT LIMIT MET'
                }
});

In this code, the prevVal in my project will be updated to 'DIGIT LIMIT MET', but I want it to be the last currentVal which is prevState.currentVal.

this.setState({
                    prevVal: this.state.currentVal,
                    currentVal: 'DIGIT LIMIT MET'

});

In this code , the problem will not appear, but according to React document, it can not make sure that the this.state.currentVal is the prevState.currentVal.

How to solve this problem perfectly? Am I misunderstanding something?

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

0

 this.setState(
      prevState => {
        const prev = prevState.currentVal;
        return {
          prevVal: prev,
          currentVal: 'DIGIT LIMIT MET'
        };
      },
      () => console.log(this.state)
    );

try this.

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