I am migrating a react app from component to functional with hooks. The problem I'm facing is that we have a nested setState using callbacks. useState doesn't have this feature. I know I could use useEffect but there are functions that fire different functions depending on who triggered the change of a single state like the ones below. These can be isolated using the callback of setState but I'm wondering how to convert such using hooks. The only thing I have in mind is nesting a useEffect which is a bad idea based on the docs, you shouldn't loop/nest hooks.
setState({
state1: "",
state2: "",
}, () => {
setState({
state3: ""
}, () => {
function 1
})
})
setState({
state1: "",
state2: "",
}, () => {
setState({
state3: ""
}, () => {
function 2
})
})
setState
? Why not just do both state changes in the same call? That way you only need the oneuseEffect
, the final done after the state changes. – Jayce444