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

128
Views
React component doesn't re-render on first prop-change

I am new to React and I am trying to build a hangman game. At the moment I am using a hardcoded list of words that the program can choose from. So far everything worked great, but now I am trying to reset the game and the react component that should rerender upon one click only re-renders after two clicks on the reset button and I don't know why

these are the states that I am using :

function App() {
  const [numberInList, setNumberInList] = useState(0)
  const randomWordsList = ["comfort", "calm", "relax", "coffee", "cozy"];
  const [generatedWord, setGeneratedWord] = useState(
    randomWordsList[numberInList]
  );
const [generatedWordLetters, setGeneratedWordLetters] = useState(
    randomWordsList[numberInList].split("").map((letter) => {
      return { letter: letter.toUpperCase(), matched: false };
    })
  );
function resetGame(){
    
    setNumberInList(prev => prev + 1)

    setGeneratedWord(randomWordsList[numberInList])

    setGeneratedWordLetters(
      generatedWord.split("").map((letter) => {
        return { letter: letter.toUpperCase(), matched: false };
      })
    );

    setFalseTries(0)
  }

this is the reset function I am using within teh function every state gets updated correctly apart from the generatedWordLetters state, which only gets updated upon clicking the reset button two times.

I can't seem to solve this problem on my own, so any help is appreciated!

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Please check useEffect on React. You can use boolean flag as state, put the useEffect parameters like below

React.useEffect(() => {
    // here your code works
},[flag])

flag is your boolean state when it changes on reset function, your component re render

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!