Add memberships to your Webflow project in minutes.
Over 200 free cloneable Webflow components. No sign up needed.
Add memberships to your React project in minutes.
Every component in React has a life cycle that can be monitored and manipulated. These life cycles can be divided into three main phases and they are the Mounting, Updating and Unmounting phase.
The mounting or birth of a React component starts off the life cycle of the component. This is the phase in which we configure the state of the application and display the UI. It is during this stage that our props are defined. The methods used during the mounting phase includes constructor(), render(), componentDidMount() and getDerivedStateFromProps()
After the birth of the component in the mounting stage, the application is then ready for the Updating stage. At this stage, data updates are received and user actions are acted on. Updating Methods include shouldComponentUpdate(), render(), getSnapshotBeforeUpdate() and componentDidUpdate(). A component is said to be updated when a change occurs in the state or props.
The Unmounting also known as the death of a React component, is the phase where our component is removed from the DOM. The method used in this phase is the componentwillUnmount() method
In this article, we are going to take a deep look at componentDidUpdate() which is one of the methods in the Updating phase.
What is the componentDidUpdate() method
The componentDidUpdate() method is an update method that is invoked after the componentDidMount() method and this happens immediately after an update occurs and not for the initial render.
It should be noted that it's not exactly 100% true and correct to say that componentDidUpdate() is called after componentDidMount().
The componentDidMount() is called in the Mounting phase, while componentDidUpdate() is called in the Updating phase. Meaning that some methods like getSnapshotBeforeUpdate() might be called between componentDidMount() and componentDidUpdate().The update can be whenever a state mutation occurs or whenever there are changes to props.
Whenever a mutation or change happens, the componentDidUpdate() method gets the previous state and the previous props as arguments. By getting the previous state and previous props as arguments, this method allows us to check whether a condition has been met and then carry out a specific action if the check returns true.
The syntax of this method is:
Most times and on very rare occasions, the componentDidUpdate() method will be called after the getSnapshotBeforeUpdate() method and when this happens, a third argument -snapshot- is passed into the componentDidUpdate() method:
How “not” to use componentDidUpdate() method
You should not set an unchecked state inside the componentDidUpdate() method. An example of an unchecked state is shown below:
The reason for this is that if we used the above code, we would enter into an infinite render loop because we first render then call update which then sets the state thereby triggering another render and so on.
The solution for this is to wrap your state in conditional statements and then to only change the state in specific areas as we will see in the example below:
Examples of componentDidUpdate()
In the example below, we have a click handler which is triggered whenever a button is clicked in the application which then changes the state of the clicked to the opposite of the current state.
From the example above, we see that the componentDidUpdate() method gets the previous state as a parameter and then checks to make sure that the previous state is the same as the current state of clicked and then logs the appropriate message to the console. if the state wasn't updated, that message would be different.
UseCases of the componentDidUpdate() method
Working with the DOM
One of the ways we can make use of the componentDidUpdate() method is when working with the DOM. Since the componentDidUpdate() method gets called after the Mounting phase, it means that we can also access DOM nodes in it. We can make use of the method to operate on the DOM when the component has been updated as we can see in the example below:
From the above example we can see the Mounting and Updating phase of our component lifecycle. We created our application’s state, then we called the componentDidMount() method, updated our state and then set a timeout so that we can keep track of our component lifecycle. The componentDidUpdate() method was called after the componentDidMount() method. In it, we got our previous state and previous props as arguments. Then we checked if the previous state is not equal to the current state. As the condition returned true, we accessed the DOM and set the inner HTML of the div to a message of our choice.
Fetching data from Server / API Calls
Another use case for the componentDidUpdate() method is making server calls or querying an API endpoint. The reason this method is useful for this is because it fetches more data without refreshing the page, after the initial fetch on page load.
Below is a link to a codesandbox which has the complete code :
tps://codesandbox.io/s/blissful-breeze-w12rps
In the example above, we created a fetch function that emulates the use of fetch or axios. In the componenDidUpdate() method, we check if the person’s name has changed by comparing the previous props with the current props. If there is a change in the person’s name, we request a list of projects and then we update the component’s state with the projects
It should be noted that we don't update the component’s internal state so as to avoid an infinite update loop and that's why we check the name of the person in props to avoid that.
Conclusion
In this article, we have taken a good look at the componentDidUpdate method, its quirks, examples and use cases. We also looked at bad practices when working with the componentDidUpdate method and solutions to them. I hope this article provides answers to questions you may have about the componentDidUpdate() method while providing a better understanding of how it works.