In the class component, the setState()
method can take a callback function, but in a functional component when I give a callback to costume setState this warning occurs:
Warning: State updates from the useState()
and useReducer()
Hooks don't support the second callback argument. To execute a side effect after rendering, declare it in the component body with useEffect()
.
I need my state set, and then the page will redirect. But I don't have any idea.
Instead of passing a callback
function, use useEffect
hook, and do something like this to achieve the desired result.
useEffect(() => {
console.log('state changed', your-state-variable)
// write your callback function here
}, [your-state-variable]);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With