Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to manage two functions inside useEffect

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);
    }, []);

like image 205
neoslo Avatar asked Jan 17 '26 11:01

neoslo


1 Answers

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)
    }, []);

like image 141
Moinul Hossain Avatar answered Jan 20 '26 01:01

Moinul Hossain



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!