react-use/docs/useMountedState.md
xobotyi 5350e8d9ee useMountedState as a replacement for useRefMounted:
- it has more obvious name;
 - returns a function that check mount state -> more handy ti use due to less to write;
 - has tests;
2019-07-31 00:05:12 +03:00

484 B

useMountedState

Lifecycle hook providing ability to check component's mount state.
Gives a function that will return true if component mounted and false otherwise.

Usage

import * as React from 'react';
import {useMountedState} from 'react-use';

const Demo = () => {
  const isMounted = useMountedState();

  React.useEffect(() => {
    setTimeout(() => {
      if (isMounted()) {
        // ...
      } else {
        // ...
      }
    }, 1000);
  });
};