When is the componentWillUnmount method called?
Last Updated :
28 Apr, 2025
The componentWillUnmount() method allows us to execute the React code when the component gets destroyed or unmounted from the DOM (Document Object Model). This method is called during the Unmounting phase of the React Life-cycle i.e. before the component gets unmounted.
Prerequisites:
The primary purpose of componentWillUnmount
is to handle cleanup operations associated with a component before it is removed from the DOM.
When the method will be called?
componentWillUnmount()
is called automatically by React just before a component is unmounted from the DOM. This can happen in various scenarios:
- Explicit Unmounting: When a parent component removes a child component from its render tree explicitly.
- Conditional Rendering: When a component's rendering logic changes such that the component is no longer rendered by its parent, it will be unmounted.
- Routing Changes: In React applications using routers like React Router, components may be unmounted when navigating to a different route.
Steps for Creating React Application:
Step 1: Create a react application using the following command.
npx create-react-app foldername
Step 2: Once it is done, change your directory to the newly created application using the following command.
cd foldername
Project Structure:

The updated dependencies in package.json file will look like:
"dependencies": {
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-scripts": "5.0.1",
"web-vitals": "^2.1.4"
}
Example : In this example, we will learn about componentWillUnmount and using a button we will unmount a component, and display alert.
JavaScript
//App.js
import React from 'react';
class ComponentOne extends React.Component {
// Defining the componentWillUnmount method
componentWillUnmount() {
alert('The component is going to be unmounted');
console.log("Component Unmounted")
}
render() {
return (
<div>
<h2 style={{ color: 'green' }}>GeeksforGeeks</h2>
<h2>When is the componentWillUnmount method called?</h2>
</div>
)
}
}
class App extends React.Component {
state = { display: true };
delete = () => {
this.setState({ display: false });
};
render() {
let comp;
if (this.state.display) {
comp = <ComponentOne />;
}
return (
<div>
{comp}
<button onClick={this.delete}>
Delete the component
</button>
</div>
);
}
}
export default App;
Step to Run Application: Run the application using the following command from the root directory of the project:
npm start
Output:

Similar Reads
What is ComponentWillMount() method in ReactJS ? ReactJS requires several components to represent a unit of logic for specific functionality. The componentWillMount lifecycle method is an ideal choice when it comes to updating business logic, app configuration updates, and API calls. PrerequisitesReact JSReact JS class componentsComponentWillMoun
4 min read
Explain the purpose of the componentWillUnmount() method? Every React Component has a lifecycle of its own, the lifecycle of a component can be defined as the series of methods invoked in different stages of the componentâs existence. Mounting and Unmounting refer to key lifecycle phases that occur during creating and removing components or elements in a w
3 min read
ReactJS componentWillUnmount() Method In React, lifecycle methods allow you to manage the behaviour of components at different stages of their existence. One important lifecycle method for cleaning up resources and side effects is componentWillUnmount(). This method is called just before a component is removed from the DOM, making it an
5 min read
ReactJS UNSAFE_componentWillMount() Method The componentWillMount() method invokes right before our React component gets loaded or mounted in the DOM (Document Object Model). It is called during the mounting phase of the React Life-cycle, i.e., before render(). It is used to fetch data from outside the component by executing the React code s
3 min read
ReactJS componentDidMount() Method In React, componentDidMount() is a lifecycle method in React that is called once a component has been rendered and placed in the DOM. This method is invoked only once during the lifecycle of a component, immediately after the first render, which is why it is useful for operations like fetching data,
7 min read