Skip to content

Instantly share code, notes, and snippets.

@meetzaveri
Created November 28, 2020 11:53
Show Gist options
  • Save meetzaveri/5dc95e6918176b50e489275bbfd2a863 to your computer and use it in GitHub Desktop.
Save meetzaveri/5dc95e6918176b50e489275bbfd2a863 to your computer and use it in GitHub Desktop.
Hook for listening for unmounting for component
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