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

132
Views
Why does my value reset after subtraction?

I'm trying to write a function that sets a value with the result of a subtraction.

The subtracted value is an input from the user that is stored in a variable. Anyway, the correct number is rendered only for a fraction of a second, and then the old value is displayed again.

Do you have any ideas? Thanks!

const addScorePlayer1 = () => {
    if (isNaN(player1ScoreInput)) setPlayer1Score(player1Score);
    else {  
        setPlayer1Score(player1Score - player1ScoreInput);
    }
  };

This input sets the subtracted value:

 <input
          type="text"
          placeholder={`Enter ${player1} score:`}
          onChange={(e) => {
            setPlayer1ScoreInput(e.target.value);
          }}
        />

This is the component where addScorePlayer1 is called:

<div className={`game-board ${newGameIsVisible}`}>
      <div className="div1">
        <h1 className="points">{player1Score}</h1>
        <h4 className="player-name">{player1}</h4>
      </div>
      <div className="div2">
        <h1 className="points">{player2Score}</h1>
        <h4 className="player-name">{player2}</h4>
      </div>
      <div className="div3">
        <input
          type="text"
          placeholder={`Enter ${player1} score:`}
          onChange={(e) => {
            setPlayer1ScoreInput(e.target.value);
          }}
        />
        <ArrowCircleRightRoundedIcon
          className="btn-arrow"
          onClick={addScorePlayer1}
        />
      </div>
      <div className="div4">
        <input
          type="text"
          placeholder={`Enter ${player2} score:`}
          onChange={(e) => {
            setPlayer2ScoreInput(e.target.value);
          }}
        />
        <ArrowCircleRightRoundedIcon className="btn-arrow" />
      </div>
      <div className="div5">
        <p>CIAO</p>
      </div>
      <div className="div6">
        <p>CIAO</p>
      </div>
      <div className="div7">
        <p>CIAO</p>
      </div>
      <div className="div8">
        <p>CIAO</p>
      </div>
    </div>
about 4 years ago · Juan Pablo Isaza
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!