0

I'm trying to fetch the advertisers list when the component mounts, but it is causing me memory leak, Any suggestions ? i'll be grateful :)

 useEffect(() => {
    const fetchData = async () => {
      const list = await Api.listAdvertisers();
      setAdvertisers(list);
    };
    fetchData();
  }, []);
skyboyer
  • 22,209
  • 7
  • 57
  • 64

2 Answers2

0

You can declare your fetchData function on main scope and you can check the function if it is called or not by declaring a didMount variable in useEffect scope.

const didMount = useRef(false);

const fetchData = async () => {
      const list = await beeswaxService.listAdvertisers();
      setAdvertisers(list);
      return;
    };

useEffect(() => {
    if(!didMount.current){
       didMount.current = true;
       fetchData();
    }
    
  }, []);
emre
  • 51
  • 4
-1

Maybe the function inside the useEffect is causing memory leak, try this?

const fetchData = async () => {
      const list = await beeswaxService.listAdvertisers();
      setAdvertisers(list);
      return;
    };

useEffect(() => {
    fetchData();
  }, []);
Zulfiqar Ali
  • 269
  • 1
  • 3
  • 12