Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

169
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda