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.

useMounted.js 967B

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