Bin
2025-12-16 9e0b2ba2c317b1a86212f24cbae3195ad1f3dbfa
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
import { useEffect, useRef } from "react";
 
/**
 * Protects async tasks from causing memory leaks in other effects/callbacks.
 * Wrap any set states within a component with
 *
 * if (mounted.current) {  ...  }
 */
export const useMounted = () => {
  const mounted = useRef(true);
 
  useEffect(() => {
    mounted.current = true;
    return () => {
      mounted.current = false;
    };
  }, []);
 
  return mounted;
};