Skip to content

Instantly share code, notes, and snippets.

@scorsi
Created February 27, 2020 15:07
Show Gist options
  • Save scorsi/5fd57ba892c5bf11e87f07b0e2286af6 to your computer and use it in GitHub Desktop.
Save scorsi/5fd57ba892c5bf11e87f07b0e2286af6 to your computer and use it in GitHub Desktop.
A hook useful to know if the current component is still mounted
import { useEffect, useRef } from 'react';
export default () => {
const isMounted = useRef(null);
useEffect(() => {
isMounted.current = true;
return () => { isMounted.current = false; };
}, []);
return isMounted;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment