🚀 LEVEL UP TO SENIOR:Unlock 500+ Advanced Practical Challenges & Expert Masterclasses.
🎓 COURSERA PARTNER:Earn professional Google, Meta, and IBM certificates to supercharge your resume.
REFERENCEreact

react Documentation

LOADING ENGINE...

React ComponentDidMount

Master React components, hooks, and best practices.

ComponentDidMount

Author

Pascual Vila

Frontend Instructor.

componentDidMount is a lifecycle method of class components in React. It runs once after the component has been mounted to the DOM. It is useful for loading data, setting up subscriptions, or performing other initialization operations.

Example of componentDidMount:

          {`import React, { Component } from "react";
          
          class Clock extends Component {
            constructor(props) {
              super(props);
              this.state = { time: new Date().toLocaleTimeString() };
            }
            
            componentDidMount() {
              this.intervalId = setInterval(() => {
                this.setState({ time: new Date().toLocaleTimeString() });
              }, 1000);
            }
            
            componentWillUnmount() {
              clearInterval(this.intervalId);
            }
            
            render() {
              return <h1>{this.state.time}</h1>;
            }
          }
          
          export default Clock;`}