Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

296
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!