What cleanup functions can be used with this useEffect hook?
P粉364642019
P粉364642019 2023-09-15 16:37:58
0
1
516

I can't figure out what cleanup function to apply in this useEffect hook. It works fine without the cleanup function.

useEffect(() => {
            const fetchProfileUser = async () => {
                if (profileUserId === existingUser._id) {
                    setprofileUser(existingUser);
                } else {
                    const profileUser = await UserApi.getUser(profileUserId);
                    setprofileUser(profileUser);
                }
            };
            fetchProfileUser();
        });

P粉364642019
P粉364642019

reply all(1)
P粉471207302

You don't always need a cleanup function. But most of the time, in useEffect you need a dependency array, even if it is empty. If you're doing requests, you definitely need a dependency array.

If you are making a request, you can use AbortController to cancel the request and use the cleanup function if the component is uninstalled.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template