Why my currentQuestion(variable) not getting updated here:


  const [currentQuestion,setCurrentQuestion]=useState(0);
  useEffect(()=>{
    console.log(currentQuestion)       //checking currentQuestion
  },[currentQuestion])

  function func_setter(id){
     if(id<3)
     {
       id=id+1;
       setCurrentQuestion(id);  //changing the id
     }
     
  }

   <Quiz problem={questions[currentQuestion]} function_call={()=>{func_setter()}}></Quiz>
          //function_call is a prop passed to Child Component to  change the currentQuestion upon userClick

 <Button color="success" className="centerer" onClick={(e)=>     //button is in the child Component of App.js
{props.function_call(props.problem.id)}}>Save and Next</Button>  //button to change the question

Here my Child Component isnt able to change the state of a variable present in the parent component..

Source: React – Stack Overflow

November 12, 2021
Category : News
Tags: react-component | react-props | reactjs | use-effect | use-state

Leave a Reply

Your email address will not be published. Required fields are marked *

Sitemap | Terms | Privacy | Cookies | Advertising

Senior Software Developer

Creator of @LzoMedia I am a backend software developer based in London who likes beautiful code and has an adherence to standards & love's open-source.