I'm using useEffect to fetch data. In my react use effect I have a function profileApprove(false) and a refresh() inside of the same useEffect. I do not want the refresh to start until profileApprove(false) is completed. At the moment I have a setTimeOut to handle the wait for profileApprove for me. I am getting what I want with the setTimeOut but unsure how to handle this properly.
export default function Profile() {
const {
profileApproved
} = profileContext();
function refresh() {
dispatch({ type: "FECTH_PROFILE", payload: { loading: true } });
}
React.useEffect(() => {
profileApproved(false);
setTimeout(() => {
refresh();
}, 1000);
}, []);
Does your profileApprove method return a promise when done? If so just await on it before calling refresh
export default function Profile() {
const {
profileApproved
} = profileContext();
function refresh() {
dispatch({ type: "FECTH_PROFILE", payload: { loading: true } });
}
React.useEffect(() => {
profileApproved(false).then(refresh)
}, []);
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