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

131
Vistas
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 Respuestas
Responde la pregunta

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 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