Created
November 28, 2020 11:53
-
-
Save meetzaveri/5dc95e6918176b50e489275bbfd2a863 to your computer and use it in GitHub Desktop.
Hook for listening for unmounting for component
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React, { useEffect } from 'react'; | |
const ProfileContainer = () => { | |
// generally I prefer this as useEffect with empty args[] | |
useEffect(() => { | |
fetch('/api/user') // perform any side effects or network requests | |
// you can have return() for this function which will be executed once component will unmount | |
return () => { | |
console.log('Unmounting ...'); | |
doYourUnmountProcess(); | |
} | |
}, []); // <-- empty array means 'run once' | |
return <div> Hello World </div>; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment