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

105
Views
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 answers
Answer question

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 Report

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