Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use state callback function equivalent on Hooks

Tags:

reactjs

I'm basically new to this react hook things, and I'm quite a bit confused. I know that react don't immediately update the state every time setState is called, but the setState method on the class-based component has a callback, which will be called after the state updated. I'm looking for a similar thing on Hook but found nothing yet.

like image 677
Ilham Syukur Avatar asked Nov 20 '25 02:11

Ilham Syukur


1 Answers

You can use useEffect hook to call a function whenever a value is updated

const [value, setValue] = useState(initialValue);

useEffect(() => {
   console.log('Print updated value and do some processing here', value);
}, [value]);
like image 180
Zohaib Ijaz Avatar answered Nov 22 '25 16:11

Zohaib Ijaz