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?
setScorein yourPopuprender. You can't do that. If you need to update your state when a variable changes use theuseEffecthook