The Unmount Lifecycle
The unmount
lifecycle lives outside of the main lifecycle, as it only comes into play when a component is removed from the DOM. This lifecycle is also far more limited, as the only lifecycle method it contains is componentWillUnmount()
.
componentWillUnmount()
componentWillUnmount()
exists solely to perform and react to the component being removed from the DOM, and then is eventually removed entirely from existence. While it sounds a bit dramatic, it's important to note that this is what happens when a component is otherwise completely destroyed. It takes no arguments, and there are some caveats about using it.
The code for componentWillUnmount()
might look something like this:
class App extends Component { Â Â componentWillUnmount() { Â Â Â Â alert("I've been removed!"); Â Â } Â Â render() { Â Â Â Â return (<div className="App">Hello World!</div>); Â ...