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

171
Vistas
Can someone explain how this state update is resolved?

Hi I've been learning ReactJs and I wrote these lines:

  handlerClick(i) {
    this.setState(
      (state) => (state.squares = [...state.squares, (state.squares[i] = "X")])
    );
  }

Now, I know whats happening but I'm confused with how this is working, the order that each operation is being executed...

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

0

Regarding the order of operations, everything to the right of an assignment operator is evaluated before the assignment takes place, so every usage of state in the expression to the right of =, refers to state before any assignment occurs.


Regarding proper usage of setState, you shouldn't assign to your old state, but rather return an entirely new object:

handlerClick(i) {
  this.setState((state) => {
    const squares = [...state.squares];
    squares[i] = "X";
    return {...state, squares };
  });
}

Or, equivalently:

handlerClick(i) {
  const squares = [...this.state.squares];
  squares[i] = "X";
  this.setState({...this.state, squares});
}
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