<div id="root"></div>


const useIsMountedRef = () => {
	const isMounted = React.useRef(false);
	React.useEffect(() => {
    console.log("I will do that")
		isMounted.current = true;
		return () => (isMounted.current = false);
	}, []);
	return isMounted;
};

function Child() {
  React.useEffect(() => {
    console.log("child")
  },[])
  return (
  
  <h1> Child</h1>
  )
}

function App() {
  const isMountedRefApproach = useIsMountedRef()
  
  React.useEffect(() => {
    if(isMountedRefApproach.current) {
         console.log("I am mounted")
    }
  }, [isMountedRefApproach.current])
  
  React.useEffect(() => {
    console.log("parent")
  },[])
  
  console.log("isMountedRefApproach.current", isMountedRefApproach.current)
  
  return <div><Child /></div>
}


ReactDOM.render(<App />, document.getElementById("root"))
View Compiled
Run Pen

External CSS

This Pen doesn't use any external CSS resources.

External JavaScript

  1. https://cdnjs.cloudflare.com/ajax/libs/react/16.10.2/umd/react.production.min.js
  2. https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.10.2/umd/react-dom.production.min.js