ComponentDidMount

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;`}