skip to Main Content

i am updating my state on onClick Func but its not updating ,

This is my State

const [loading, setLoading] = useState(false);

my OnCLick func

 const handleBookmarkButtonClick = () => {
    setLoading(true);
    //rest code...including an async func
  };

console.log("loading":loading);

the output i got

Object {loading: false }

my OnCLick func

const handleBookmarkButtonClick = () => {
    setLoading(true);
    //rest code...including an async func
};

console.log("loading":loading);

i am not getting true ,

2

Answers


  1. Create state like this:

    const [loading, setLoading] = useState(false); 
    

    And update state like this:

     const handleBookmarkButtonClick = () => { 
      setLoading(true);
     //rest code...including an async func
     };
    
    Login or Signup to reply.
  2. Make sure that:

    • The setState is not affected by the async function (try to call the setState separately in the function).
    • No other states are not affecting the loading state
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search