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

226
Visualizações
How can I stop the React buttons from rendering synchronously?

Every time I click the "increase" or "decrease" buttons for a certain player, I want the score to increase for that player only. However, right now when I click either of the buttons it increases both scores. I know it is probably something simple that I am completely overlooking, but any help is appreciated. Thank you in advance! Here is my code https://codesandbox.io/s/objective-curran-6ogn3?file=/src/App.js:0-2090:

import "./styles.css";
import {Component, useState} from 'react';

export default function App() {
  const [count, setCount] = useState(0);

  const [playerScore, setplayerScore] = useState([{
    nameOne: 'Player 1'
  }, {
    nameTwo: 'Player 2'
  }
]);

  const increase = () => {
    setCount(count + 1);
    setplayerScore(playerScore);
  };

  const decrease = () => {
    setCount(count - 1);
    setplayerScore(playerScore);
  };

  return (
    <main>
    {/* player 1*/}
      <div>
          {playerScore.map((player) => {
            return (
              <div>
                <h2 className="name">{player.nameOne}</h2>
              </div>
            );
          })}
      </div>
      <div>
        <div className="score">{count}</div>
        {/* increase button */}
          <button onClick={increase}>
            Increase
          </button>

        {/* decrease button */}
          <button onClick={decrease}>
            Decrease
          </button>
      </div>

    {/* player 2 */}
    <div>
          {playerScore.map((player) => {
            return (
              <div>
                <h2 className="name">{player.nameTwo}</h2>
              </div>
            );
          })}
      </div>
      <div>
        <div className="score">{count}</div>
        <section>
          <button onClick={increase}>Increase</button>
          <button onClick={decrease}>Decrease</button>
        </section>
      </div>
    </main>
  );
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

I did this fork based on your code. https://codesandbox.io/s/cranky-hooks-q9ntu?file=/src/App.js

I have refactored some logic and added the functionality that I understand you want. Specially the increase/decrease logic.

The main error was that you were using same state for the entire array, so the value will always be the same for all the items.

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