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

132
Vistas
How the Setstate work behind the scenes in React
import React,{Component} from "react";
class Message extends Component{
constructor()
{
    super()
    this.state={
        message:0
    }
  
}
changemessage()
{
    this.setState(
        prevState=>({
            message:prevState.message+1
        }
    ),
    ()=>
        {
           console.log( 'callback value',this.state.message)

        }
    )
   
}
sample()
{
    this.setState(
        prevState=>({
            message:this.state.message+1
        }
    ),
        ()=>
        {
           console.log( 'callback value',this.state.message)

        }
    )
   
}

convert()
{
    this.changemessage()
    this.sample()
    this.changemessage()
    this.sample()
    this.changemessage()
    this.sample()
    this.changemessage()
    this.sample()
    this.changemessage()

}
render()
{
    return(
        <div>
        <h1 >{this.state.message}</h1>
        <button onClick={()=>{this.convert()}}>Subscribe</button>
        </div>
    )
}
}

To understand the SetState i used two function with prev state and this.state.I expect 9 as answer .but i get 2 as a answer printed in console for 9 times.if i use Prev State in both function instead of this.state.message in sample function i got 9 as a answer.i want to know what happening in the background if i use like this.

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