Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

143
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda