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>); ...