I want to execute a function after functional component setstate has been completed. There are two ideas where we can use to know the state update has been completed
- Using useEffect
- use-state-with-callback (But the callback is not called as expected.)
We can use useEffect to do a correct approach. But we cannot use in all the situation. For example
const [count, setCount] = useState(0);
const func1 = ()=>{}
const func2 = ()=>{}
const func3 = ()=>{}
The setCount has been called inside func1 and func3. After setCount in func1, I need to call func2 but not in func3. If I use useEffect, whenever count has been updated the func2 will be called.
So I need a proper callback which need to be called after setState.
Can I execute a function after setState is finished updating?