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

108
Vistas
Warning: Cannot update a component (`App`) while rendering a different component

I have a function called setScore( ) in my APP component which is passed down as props to my Popup Component. I want to call the UpdateScore( ) function based on a condition in my Popup Component. However, I also want to call the setScore( ) method based on a condition but it gives me the error listed above.

const Popup = ({user,correctLetters,wrongLetters,setPlayable,selectedWord, playAgain, currentQuestion, words, score, setScore, quitGame}) => {

    function UpdateScore() {

            if(!check){
                check = true;    
                setScore(score => (score + 1));               
            }    
    };

    if ((checkWin(correctLetters,wrongLetters,selectedWord) === 'win') && (currentQuestion < words.length -1)){
        UpdateScore();
    } 
    else if((checkWin(correctLetters,wrongLetters,selectedWord) === 'win') && (currentQuestion === words.length -1 )){
        UpdateScore();
    }

This isn't the complete code but the main issue is with the setState( ) call. I've looked at other posts and they resolved it by wrapping the setState method in a useEffect but then it gives me another error :

Cannot Conditionally render useEffects.

Any idea how I can resolve this?

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

Probably the following works:

const isWin =
  checkWin(correctLetters, wrongLetters, selectedWord) ===
    'win' &&
  currentQuestion <= words.length &&
  !check;
React.useEffect(() => {
  if (isWin) {
    check = true;
    setScore((score) => score + 1);
  }
}, [isWin, setScore]);

I do see a problem with check and have a feeling it should be in a useRef.

about 4 years ago · Juan Pablo Isaza Denunciar

0

The problem is that you are calling a UpdateScore (that calls setScore) in component's body and this is not how React works. If you want to execute if...else if at Popup first render, use an useEffect hook in this way:

useEffect(() => {
    if ((checkWin(correctLetters,wrongLetters,selectedWord) === 'win') && (currentQuestion < words.length -1)){
        UpdateScore();
    } 
    else if((checkWin(correctLetters,wrongLetters,selectedWord) === 'win') && (currentQuestion === words.length -1 )){
        UpdateScore();
    }
}, [correctLetters, wrongLetters, selectedWord, currentQuestion, UpdateScore]);

Actually, this useEffect will be triggered every time one of correctLetters, wrongLetters, selectedWord, currentQuestion, UpdateScore changes his value (but you need these as dependencies otherwise you will get a warning on "missing dependencies").

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