How to stop memory leak in useEffect hook react How to stop memory leak in useEffect hook react reactjs reactjs

How to stop memory leak in useEffect hook react


With useEffect you can return a function that will be run on cleanup. So in your case, you'll want something like this:

useEffect(() => {  let unmounted = false;  setPageLoading(true);  props    .dispatch(fetchCourses())    .then(() => {      if (!unmounted) {        setPageLoading(false);      }    })    .catch((error: string) => {      if (!unmounted) {        toast.error(error);        setPageLoading(false);      }    });  return () => { unmounted = true };}, []);

EDIT: if you need to have a call that's kicked off outside of useEffect, then it will still need to check an unmounted variable to tell whether it should skip the call to setState. That unmounted variable will be set by a useEffect, but now you need to go through some hurdles to make the variable accessible outside of the effect.

const Example = (props) => {  const unmounted = useRef(false);  useEffect(() => {    return () => { unmounted.current = true }  }, []);  const setFilter = () => {    // ...    props.dispatch(fetchCourses()).then(() => {      if (!unmounted.current) {        setLoading(false);      }    })  }  // ...  return (    <ReactTable onFetchData={setFilter} /* other props omitted */ />  );}


The other answers work of course, I just wanted to share a solution I came up with.I built this hook that works just like React's useState, but will only setState if the component is mounted. I find it more elegant because you don't have to mess arround with an isMounted variable in your component !

Installation :

npm install use-state-if-mounted

Usage :

const [count, setCount] = useStateIfMounted(0);

You can find more advanced documentation on the npm page of the hook.


Memory leak happens, when a thing that is unnecessary and is supposed to be cleared from memory is kept because some other thing is still holding it. In React Component case, the async call made in component may hold the references of setState or other references and will hold them until the call completes.The warning you see is from React saying that something is still holding and setting state of a component instance that was removed from tree long back when component unmounted. Now using a flag to not set the state only removes the warning but not the memory leak, even using Abort controller does the same. To escape this situation you can use state management tools that helps dispatching an action which will do processing out side of component without holding any memory references of the component, for example redux. If you are not using such tools then you should find a way to clear the callbacks you pass to the async call (then, catch, finally blocks) when component unmounts. In the below snippet I am doing the same detaching the references to the methods passed to async call to avoid memory leaks.Event Emitter here is an Observer, you can create one or use some package.

const PromiseObserver = new EventEmitter();class AsyncAbort {  constructor() {    this.id = `async_${getRandomString(10)}`;    this.asyncFun = null;    this.asyncFunParams = [];    this.thenBlock = null;    this.catchBlock = null;    this.finallyBlock = null;  }  addCall(asyncFun, params) {    this.asyncFun = asyncFun;    this.asyncFunParams = params;    return this;  }  addThen(callback) {    this.thenBlock = callback;    return this;  }  addCatch(callback) {    this.catchBlock = callback;    return this;  }  addFinally(callback) {    this.finallyBlock = callback;    return this;  }  call() {    const callback = ({ type, value }) => {      switch (type) {        case "then":          if (this.thenBlock) this.thenBlock(value);          break;        case "catch":          if (this.catchBlock) this.catchBlock(value);          break;        case "finally":          if (this.finallyBlock) this.finallyBlock(value);          break;        default:      }    };    PromiseObserver.addListener(this.id, callback);    const cancel = () => {      PromiseObserver.removeAllListeners(this.id);    };    this.asyncFun(...this.asyncFunParams)      .then((resp) => {        PromiseObserver.emit(this.id, { type: "then", value: resp });      })      .catch((error) => {        PromiseObserver.emit(this.id, { type: "catch", value: error });      })      .finally(() => {        PromiseObserver.emit(this.id, { type: "finally" });        PromiseObserver.removeAllListeners(this.id);      });    return cancel;  }}

in the useEffect hook you can do

React.useEffect(() => {    const abort = new AsyncAbort()      .addCall(simulateSlowNetworkRequest, [])      .addThen((resp) => {        setText("done!");      })      .addCatch((error) => {        console.log(error);      })      .call();    return () => {      abort();    };  }, [setText]);

I forked someones code from here to use above logic, you can check it in action in the below linklink