Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use callback of setState()? [duplicate]

I'm trying to use callback of setState() in order to check that the state was changed correctly. However, I get an error Expected 1 arguments, but got 2.

   setCurrentLength(300, () => {
      console.log('Current length ', currentLength);
   });

How to solve it? Thanks

like image 558
rumon Avatar asked Dec 06 '25 07:12

rumon


1 Answers

You are using the callback for setState. But you are using useState hook.

Use should use useEffect for this with dependency array, so when ever currentLength is changed your console.log will be executed.

useEffect(()=>{
  console.log('Current length ', currentLength);
}, [currentLength]);
like image 195
kiranvj Avatar answered Dec 08 '25 20:12

kiranvj