Dashboard sipadu mbip
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

12345678910111213141516171819202122232425262728293031323334
  1. import { useRef, useEffect } from 'react';
  2. /**
  3. * Track whether a component is current mounted. Generally less preferable than
  4. * properlly canceling effects so they don't run after a component is unmounted,
  5. * but helpful in cases where that isn't feasible, such as a `Promise` resolution.
  6. *
  7. * @returns a function that returns the current isMounted state of the component
  8. *
  9. * ```ts
  10. * const [data, setData] = useState(null)
  11. * const isMounted = useMounted()
  12. *
  13. * useEffect(() => {
  14. * fetchdata().then((newData) => {
  15. * if (isMounted()) {
  16. * setData(newData);
  17. * }
  18. * })
  19. * })
  20. * ```
  21. */
  22. export default function useMounted() {
  23. var mounted = useRef(true);
  24. var isMounted = useRef(function () {
  25. return mounted.current;
  26. });
  27. useEffect(function () {
  28. return function () {
  29. mounted.current = false;
  30. };
  31. }, []);
  32. return isMounted.current;
  33. }