setState((prevState)=>{return {value:prevState.value+1}},()=>{callback})
In the above code, you're using an anonymous function to wrap callback
, but callback
is not triggered as you expected.
If you want to trigger callback
, you need to make it like callback()
setState((prevState)=>{return {value:prevState.value+1}},()=>{callback()})
setState((prevState)=>{return {value:prevState.value+1}},callback)
This is different. You pass callback
as a parameter directly, and the callback will be executed in setState
internally.
To clarify more how setState
calls callback
internally, I'd show you a small demo.
const setState = (state, callback) => {
//TODO: The logic for state
//and then execute callback
callback()
}
setState({}, () => console.log('Callback here'))