I am developing an application where I need to make multiple API calls on page load and I need to alter the single state of the component upon each API call. The architecture looks like below.
const [stateForm, setStateForm] = useState(.....)
useEffect(()=>{
//FIRST API CALL
setStateForm({....})
},[])
useEffect(()=>{
//SECOND API CALL
setStateForm({....})
},[])
useEffect(()=>{
//THIRD API CALL
setStateForm({....})
},[])
//SO ON.....
My question is : Is it a okay to cause side effects in this way? Will altering state in each useEffect hook cause mutiple re-renders?
setStateFormdoes not merge state object assetStatemethod onComponentdid. So each call will override entirestateForm. If you need to merge state use callbacksetStateForm(prevState => ({/* merge here */}))