react js

React Lifecycle Methods: ComponentDidMount

The componentDidMount() lifecycle method is called immediately after a component is mounted or, in simple terms, when that particular component or page is in use or in focus or in the DOM.

You can then decide what should happen when that page loads. If you’re fetching data from a remote endpoint or your application’s functionality relies heavily on this type of data, componentDidMount() is a good place to instantiate your network request.

Mounting a Component

When mounting a component, there are about four (4) in-built methods that get called in a particular order. These are:

  1. constructor()
  2. getDerivedStateFromProps()
  3. render()
  4. componentDidMount()

constructor()

The constructor method is the first that’s called before anything else in your React Component, and this is where initial states are usually set up for use by the application.

The constructor method is called with props passed in as a parameter and is always immediately followed by super before anything else is passed. This is done so that the parent’s constructor method can be initiated and will also allow the component to inherit methods from its parents.

Here’s an illustration

class YourClassName extends Component{
constructor(props){
super(props){
this.state = {
name: 'Lawson Luke'
}
}
}

render(){
return(
<div>
<p>My name is {this.state.name} </p>
</div>
)
}
}

export default YourClassName;

getDerivedStateFromProps()

The getDerivedStateFromProps() method is called just before rendering the element(s) in the DOM.

This is usually where you update or set your state based on the initial value of the props.

 static getDerivedStateFromProps(props, state) {
    return {name: props.newName };
  }

render()

The render() method is only required in a class component. When called, it examines the values you’ve passed into this.props, this.state and just outputs your HTML result or the results of your code.

componentDidMount()

This is called after the component is rendered or is available in the DOM. You may want to perform some logic here like, for example, set state value.

class YourClassName extends React.Component {
  constructor(props) {
    super(props);
    this.state = {name: "Lawson Luke"};
  } 
componentDidMount() {
    setTimeout(() => {
      this.setState({name: "Matt Hendrik"})
    }, 1000)
  }
render() {
    return (
      <h1>My name is {this.state.name}</h1>
    );
  }

export default YourClassName

Recent Posts

Google Announces that AI-developed Drug will be in Trials by the End of the Year

Isomorphic Labs, a drug discovery start-up launched four years ago and owned by Google’s parent…

1 hour ago

Instagram Extends Reels Duration to 3 Minutes

Regardless of whether TikTok faces a U.S. ban, Instagram is wasting no time positioning itself…

2 days ago

AWS Expands Payment Options for Nigerian Customers, Introducing Naira (NGN) for Local Transactions

Amazon Web Services (AWS) continues to enhance its customer experience by offering more flexible payment…

6 days ago

Why JavaScript Remains Dominant in 2025

JavaScript, often hailed as the "language of the web," continues to dominate the programming landscape…

1 week ago

Amazon Moves to Upgrade Alexa with Generative AI Technology

Amazon is accelerating efforts to reinvent Alexa as a generative AI-powered “agent” capable of performing…

1 week ago

Smuggled Starlink Devices Allegedly Used to Bypass India’s Internet Shutdown

SpaceX's satellite-based Starlink, which is currently unlicensed for use in India, is reportedly being utilized…

1 week ago