useRefMounted

Lifecycle hook that tracks if component is mounted. Returns a ref, which has a boolean .current property.

用法

  1. import {useRefMounted} from 'react-use';
  2. const Demo = () => {
  3. const refMounted = useRefMounted();
  4. useEffect(() => {
  5. setTimeout(() => {
  6. if (refMounted.currrent) {
  7. // ...
  8. } else {
  9. // ...
  10. }
  11. }, 1000);
  12. });
  13. };