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

235
Vistas
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 Respuestas
Responde la pregunta

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 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