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

298
Vistas
React Rock, paper, scissors app - not updating after each click

I'm not sure what I'm doing wrong here, clicking a button displays the result of the previous button that is being pressed, not the current button... The console displays completely different results to what im seeing on the page when i add console.logs to the handleClick function.

import { useState } from "react";
import "./App.css";
import { playRound, computerPlay } from "./utils/rps";

  function App() {
  const [playerSelection, setPlayerSelection] = useState("");
  const [CPUselect, setCPUselect] = useState("");
  const [results, setResult] = useState("");

  const handleClick = (e) => {
    setPlayerSelection(e.target.value);
    setCPUselect(computerPlay());
    setResult(playRound(playerSelection, CPUselect));
  };
  return (
    <>
      <div>
        <h1>RPS!</h1>
        <div>
          <p>You Chose {playerSelection}</p>
          <p>Computer Chose {CPUselect}</p>
          <p>{results}</p>
          <p>
            <button value={"Rock"} onClick={handleClick}>
              Rock
            </button>
            <button value={"Paper"} onClick={handleClick}>
              Paper
            </button>
            <button value={"Scissors"} onClick={handleClick}>
              Scissors
            </button>
          </p>
        </div>
      </div>
    </>
  );
}

export default App;

and the JS file with functions:

export const computerPlay = () => {
  const num = Math.floor(Math.random() * 3);
  if (num === 0) {
    return "Rock";
  } else if (num === 1) {
    return "Scissors";
  } else if (num === 2) {
    return "Paper";
  }
};

export const playRound = (playerSelection, computerSelection) => {
  if (playerSelection === computerSelection) {
    return "Draw!";
  }
  if (
    (playerSelection === "Rock" && computerSelection === "Scissors") ||
    (playerSelection === "Scissors" && computerSelection === "Paper") ||
    (playerSelection === "Paper" && computerSelection === "Rock")
  ) {
    return "You win! " + playerSelection + " beats " + computerSelection;
  } else {
    return "You Lose! " + computerSelection + " beats " + playerSelection;
  }
};
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

React batches state changes, they do not happen in a predictable synchronous way. You need to wait for React to actually change the state before computing the score and use the setResult function.

This is where the useEffect hook comes in handy. The first argument is a callback and the second is dependency list. Basically, whenever an element of the list changes (the playerSelection and the CPUselect value in this case), React is going to call the function passed to the useEffect hook.

  const [playerSelection, setPlayerSelection] = useState("");
  const [CPUselect, setCPUselect] = useState("");
  const [results, setResult] = useState("");

  const handleClick = (e) => {
    setPlayerSelection(e.target.value);
    setCPUselect(computerPlay());
  };

  useEffect(() => {
    setResult(playRound(playerSelection, CPUselect));
  }, [playerSelection, CPUselect]);
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