React useEffect cleanup explained

项月亮
Aug 29, 2021

According to React official doc, most of useEffect don’t require any cleanup, but in some cases, we should cleanup useEffect hook to avoid memory leaking and some other errors like

The useEffect cleanup is built in a way that if we return a function within the method, this function will execute when the component gets disassociated. In simple terms, cleanup is used to clean up the effect we produced last time.

The simplest example of useEffect with cleaup is as following

A detailed explanation about when you should use useEffect with cleanup

When does cleanup function run?

How to fetch data with React Hooks?

--

--