Every component in React goes through a lifecycle of events. You can think is of going through a cycle of birth, growth, and death the same as the picture below.
Human and React Lifecycle
The phases are:
This is the phase in which the component is going to start its journey. The developer has to define the props and initial state of the component. This is usually done inside the constructor method (see below to understand the initialization phase better).
class Increment extends React.Component {constructor(props){super(props);// Setting the initial statethis.state = { count : 0};}}
Mounting is the phase of the component lifecycle when the initialization of the component is completed and the React component mounts on the DOM (i.e., is created and inserted into the DOM) and rendered for the first time on the webpage. It has 2 predefined functions:-
class ReactCycle extends React.Component {
componentWillMount() {
console.log('Component will mount!')
}
componentDidMount() {
console.log('Component did mount!')
this.getData();
}
getData=()=>{
/*** method to make api call***
}
render() {
return (
<div>
<h3>Hello mounting methods!</h3>
</div>
);
}
}
#react #web-development #javascript #programming #developer