---
title: "Comment by Favourite Jome on \"A Look At React Hooks: useEffect\" | Hashnode"
description: "Hello Victoria Lo , I have a question. I don&#39;t really understand why I should use a cleanup function in useEffect hook, so far all the little projects I&#39"
url: "https://hashnode.com/posts/a-look-at-react-hooks-useeffect/602e6eecbfecab23c35247a2/comment/602ec67abfecab23c3524d55"
---

[Discussion](https://hashnode.com/posts/a-look-at-react-hooks-useeffect/602e6eecbfecab23c35247a2)

Comment [A Look At React Hooks: useEffect](https://hashnode.com/posts/a-look-at-react-hooks-useeffect/602e6eecbfecab23c35247a2)

[Favourite Jome](https://hashnode.com/@Favourite)

[Feb 18, 2021](https://hashnode.com/posts/a-look-at-react-hooks-useeffect/602e6eecbfecab23c35247a2/comment/602ec67abfecab23c3524d55)

Hello [Victoria Lo](https://hashnode.com/@victoria), I have a question.

I don't really understand why I should use a cleanup function in useEffect hook, so far all the little projects I've done using React I don't return the cleanup function.

If it's not too much to ask, could you give instances where I really need to return a cleanup function and what are the side effects of not returning a cleanup function?

[JC Lee](https://hashnode.com/@ljc_dev)

[Feb 19, 2021](https://hashnode.com/posts/a-look-at-react-hooks-useeffect/602e6eecbfecab23c35247a2/comment/602ec67abfecab23c3524d55#reply-602f07a21591c5239ebdc0d3)

Stealing the spotlight 😁. Basically if u set something with useEffect u need to remove it when u don't need it. I had this problem on my 2048 game where I added an event listener inside of a useEffect and didn't clean up. useEffect(()=> { document.addEventListener("click", handleClick) }, \[\]) On re-render by a useState change for example, useEffect runs again so the same event listener gets added again and again. So my function got called too many times. To prevent this I added removeEventListener like this: useEffect(()=> { document.addEventListener("click", handleClick) return ()=> document.removeEventListener("click", handleClick) }, \[\]) Return gets called every time before the component re-renders and runs removeEventListener 👌

[Victoria Lo](https://hashnode.com/@victoria)

[Feb 19, 2021](https://hashnode.com/posts/a-look-at-react-hooks-useeffect/602e6eecbfecab23c35247a2/comment/602ec67abfecab23c3524d55#reply-602f1e751591c5239ebdc128)

Great example [JC Lee](https://hashnode.com/@ljc_dev)! Thanks for answering on my behalf :)

[Favourite Jome](https://hashnode.com/@Favourite)

[Feb 19, 2021](https://hashnode.com/posts/a-look-at-react-hooks-useeffect/602e6eecbfecab23c35247a2/comment/602ec67abfecab23c3524d55#reply-602f65991591c5239ebdc757)

Thanks for the feedback [JC Lee](https://hashnode.com/@ljc_dev) I think I get it know.

But if it's a fetch call, there nothing to clean up right?

[JC Lee](https://hashnode.com/@ljc_dev)

[Feb 22, 2021](https://hashnode.com/posts/a-look-at-react-hooks-useeffect/602e6eecbfecab23c35247a2/comment/602ec67abfecab23c3524d55#reply-6033552ff51caf4448563b95)

[Jome Favourite](https://hashnode.com/@Favourite) No clean up but u'd want to add a logic to check whether u already fetched to prevent re-fetch. Like if(data) return

[Favourite Jome](https://hashnode.com/@Favourite)

[Feb 22, 2021](https://hashnode.com/posts/a-look-at-react-hooks-useeffect/602e6eecbfecab23c35247a2/comment/602ec67abfecab23c3524d55#reply-60335f1672a689446db3fb0b)

Okay, that'll be when I want to use the data. Thanks [JC Lee](https://hashnode.com/@ljc_dev)
