3

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

rumon
  • 466
  • 2
  • 8
  • 21
  • 1
    `setState` returned as the second part of the tuple from the `useState` hook does not have this feature. The callback function you are attempting to use is part of `this.setState` found in a class Component. – Jacob Smit Jul 21 '21 at 05:10

1 Answers1

5

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]);
kiranvj
  • 32,342
  • 7
  • 71
  • 76