close
The Wayback Machine - https://web.archive.org/web/20240930165417/https://www.geeksforgeeks.org/reactjs-componentdidupdate-method/
Open In App

ReactJS componentDidUpdate() Method

Last Updated : 31 Dec, 2020
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

The componentDidUpdate() method allows us to execute the React code when the component is updated. All the network requests that are to be made when the props passed to the component changes are coded here.

The componentDidUpdate()is called after componentDidMount() and can be useful to perform some action when the state of the component changes.

Syntax:

componentDidUpdate(prevProps, prevState, snapshot)

Parameters: Following are the parameter used in this function:

  • prevProps: Previous props passed to the component
  • prevState: Previous state of the component
  • snapshot: Value returned by getSnapshotBeforeUpdate() method

Tip: To avoid an infinite loop, all the network requests are needed to be inside a conditional statement as:

componentDidUpdate(prevProps, prevState) {
  if (prevState.data !== this.state.data) {
    // Now fetch the new data here.
  }
}

Creating React Application:

Step 1: Create a React application using the following command:

npx create-react-app functiondemo

Step 2: After creating your project folder i.e. functiondemo, move to it using the following command:

cd functiondemo

Project Structure: It will look like the following.

BERJAYA

Project Structure

Example: In this example, we are going to build a React application that changes the text in the heading when the state of the component is updated.

App.js: Now write down the following code in the App.js file. Here, App is our default component where we have written our code.

Javascript




import React from 'react';
  
class App extends React.Component {
  // Defining the state
  state = {
    company: 'GFG'
  };
  
  componentDidMount() {
    // Changing the state after 600ms
    setTimeout(() => {
      this.setState({ company: 'GeeksForGeeks' });
    }, 600);
  }
  
  componentDidUpdate() {
    document.getElementById('disclaimer').innerHTML =
      'P.s:  GFG is also known as ' + this.state.company;
  }
  
  render() {
    return (
      <div>
        <h1 style={{
          margin: 'auto',
          width: '50%',
          padding: 20,
          marginTop: '10%',
          border: 'solid 1px black',
          textAlign: 'center',
          fontSize: 18,
        }}>
          Greatest Science Portal For Geeks :
          {this.state.company}
          <div id="disclaimer"></div>
        </h1>
      </div>
    );
  }
}
  
export default App;


Note: You can apply your own styling to the application.

Step to Run Application: Run the application using the following command from the root directory of the project:

npm start

Output: 

BERJAYA

Output



Similar Reads

What is the purpose of the componentDidUpdate lifecycle method ?
React web apps are actually a collection of independent components that run according to the interactions made with them. Every React Component has a lifecycle of its own, the lifecycle of a component can be defined as the series of methods that are invoked in different stages of the component’s existence. Prerequisites:NPM & NodeReact JSReact
3 min read
How useEffect replaces componentDidMount and componentDidUpdate methods?
In React, the useEffect hook can be used to replicate the behavior of both componentDidMount and componentDidUpdate lifecycle methods. Here's how you can achieve that: componentDidMount Equivalent:When you want to perform some actions immediately after the component mounts, you can use useEffect with an empty dependency array ([]). This ensures tha
2 min read
Difference in behaviour of componentDidUpdate and useEffectHook with no dependency Array
If you have Basic knowledge of React with class-based components and functional components with hooks then you know we can achieve some life cycle methods of class-based components with useEffect() hooks with its dependency array and return function. like the componentDidUpdate method, we can achieve with useeEffect with no dependency array. But do
4 min read
ReactJS getSnapshotBeforeUpdate() Method
The getSnapshotBeforeUpdate() method is invoked just before the DOM is being rendered. It is used to store the previous values of the state after the DOM is updated. Any value returned by getSnapshotBeforeUpdate() method will be used as a parameter for componentDidUpdate() method. This function is always used along with the componentDidUpdate() met
2 min read
ReactJS bind() Method
The bind() is an inbuilt method in React that is used to pass the data as an argument to the function of a class based component. Syntax: this.function.bind(this,[arg1...]);Parameter: It accepts two parameters, the first parameter is the this keyword used for binding and the second parameter is the sequence of arguments that are passed as a paramet
2 min read
ReactJS componentWillUnmount() Method
The componentWillUnmount() method allows us to execute the React code when the component gets destroyed or unmounted from the DOM (Document Object Model). This method is called during the Unmounting phase of the React Life-cycle i.e before the component gets unmounted. All the cleanups such as invalidating timers, canceling network requests, or cle
2 min read
ReactJS getDerivedStateFromError() Method
The getDerivedStateFromError() method is invoked if some error occurs during the rendering phase of any lifecycle methods or any children components. This method is used to implement the Error Boundaries for the React application. It is called during the render phase, so side-effects are not permitted in this method. For side-effects, use component
2 min read
ReactJS componentDidCatch() Method
The componentDidCatch() method is invoked if some error occurs during the rendering phase of any lifecycle methods or any children components. This method is used to implement the Error Boundaries for the React application. It is called during the commit phase, so unlike getDerivedStateFromError() which was called during the render phase, side-effe
2 min read
ReactJS forceUpdate() Method
The components in React will re-render only if the state of the component or the props passed to it changes but if we need to re-render the component if some data changes then we will use forceUpdate() method of React. Calling the forceUpdate() will forcibly re-render the component and thus calls the render() method of the component skipping the sh
2 min read
ReactJS UNSAFE_componentWillMount() Method
The componentWillMount() method invokes right before our React component gets loaded or mounted in the DOM (Document Object Model). It is called during the mounting phase of the React Life-cycle, i.e., before render(). It is used to fetch data from outside the component by executing the React code synchronously which causes our component to render
3 min read
ReactJS unmountComponentAtNode() Method
React.js library is all about splitting the app into several components. Each Component has its own lifecycle. React provides us with some in-built methods that we can override at particular stages in the life-cycle of the component. In class-based components, the unmountComponentAtNode() method Remove a mounted React component from the DOM. Creati
1 min read
ReactJS findDOMNode() Method
React.js library is all about splitting the app into several components. Each Component has its own lifecycle. React provides us some in-built methods that we can override at particular stages in the life-cycle of the component. In class-based components, the FindDOMNode() method is used to return DOM of the given element. Creating React Applicatio
2 min read
ReactJS isElement() Method
React.js library is all about splitting the app into several components. Each Component has its own lifecycle. React provides us some in-built methods that we can override at particular stages in the life-cycle of the component. In this article, we will know how to use isElement() method. The isElement() method returns true if an element is any rea
2 min read
ReactJS isCompositeComponent() Method
React.js library is all about splitting the app into several components. Each Component has its own lifecycle. React provides us some in-built methods that we can override at particular stages in the life-cycle of the component. In this article, we will see how to use isCompositeComponent() method. The isCompositeComponent() method return true if i
2 min read
ReactJS isElementOfType() Method
React.js library is all about splitting the app into several components. Each Component has its own lifecycle. React provides us some in-built methods that we can override at particular stages in the life-cycle of the component. In this article, we will know how to use isElementOfType() method. The isElementOfType() method return true if an element
2 min read
ReactJS isCompositeComponentWithType() Method
React.js library is all about splitting the app into several components. Each Component has its own lifecycle. React provides us some in-built methods that we can override at particular stages in the life-cycle of the component. In this article, we will know how to use isCompositeComponentWithType() method. This method returns true if a component w
2 min read
ReactJS isDOMComponent() Method
React.js library is all about splitting the app into several components. Each Component has its own lifecycle. React provides us some in-built methods that we can override at particular stages in the life-cycle of the component. In this article, we will know how to use isDOMComponent() method. This method returns true if the instance is a DOM compo
2 min read
ReactJS TestRenderer.toTree() Method
React.js library is all about splitting the app into several components. Each Component has its own lifecycle. React provides us some in-built methods that we can override at particular stages in the life-cycle of the component. In this article, we will know how to use TestRenderer.toTree() method. This method is used to return an object representi
2 min read
ReactJS TestRenderer.toJSON() Method
React.js library is all about splitting the app into several components. Each Component has its own lifecycle. React provides us some in-built methods that we can override at particular stages in the life-cycle of the component. In this article, we will know how to use TestRenderer.toJSON() method. This method is used to return an object representi
2 min read
ReactJS TestRenderer.create() Method
React.js library is all about splitting the app into several components. Each Component has its own lifecycle. React provides us some in-built methods that we can override at particular stages in the life-cycle of the component. In this article, we will know how to use TestRenderer.create() method. This method is used to create a TestRenderer insta
2 min read
ReactJS TestRenderer.unmount() Method
React.js library is all about splitting the app into several components. Each Component has its own lifecycle. React provides us some in-built methods that we can override at particular stages in the life-cycle of the component. In this article, we will know how to use TestRenderer.unmount() method. TestRenderer.unmount() method is used to unmount
1 min read
ReactJS testInstance.findAllTypes() Method
React.js library is all about splitting the app into several components. Each Component has its own lifecycle. React provides us some in-built methods that we can override at particular stages in the life-cycle of the component. In this article, we will know how to use testInstance.findAllTypes() method. The testInstance.findAllTypes() method is us
1 min read
ReactJS testInstance.findByProps() Method
React.js library is all about splitting the app into several components. Each Component has its own lifecycle. React provides us some in-built methods that we can override at particular stages in the life-cycle of the component. In this article, we will know how to use testInstance.findByProps() method. The testInstance.findByProps() method is used
1 min read
ReactJS testInstance.findAllByProps() Method
React.js library is all about splitting the app into several components. Each Component has its own lifecycle. React provides us some in-built methods that we can override at particular stages in the life-cycle of the component. In this article, we will know how to use testInstance.findAllByProps() method testInstance.findAllByProps() method is use
1 min read
What is ComponentWillMount() method in ReactJS ?
ReactJS requires several components to represent a unit of logic for specific functionality. The componentWillMount lifecycle method is an ideal choice when it comes to updating business logic, app configuration updates, and API calls. PrerequisitesReact JSReact JS class componentsComponentWillMount() methodThe componentWillMount() lifecycle hook i
4 min read
How re-render a component without using setState() method in ReactJS ?
In React, to re-render a class-based component with an updated state we generally use the setState() method. But instead, we can use other methods to re-render a component without using setState(). Prerequisites: NPM & Node.jsReact JSReactJS propsReactJS forceUpdate() methodApproaches to re-render a component without using setState() method are
3 min read
How The ReactJS onClick Can't Pass Value To Method?
In React, handling events like onClick is a common task, often requiring passing values to event handlers. However, developers sometimes encounter issues where the onClick event handler can't seem to pass values to the method. Understanding why this happens and how to handle it properly is essential for effective react development. When you attach
3 min read
ReactJS UNSAFE_componentWillUpdate() Method
The componentWillUpdate() method provides us the control to manipulate our React component just before it receives new props or state values. It is called just before the rendering of our component during the updating phase of the React Life-cycle ,i.e., this method gets triggered after the updation of State or Props and just before the execution o
3 min read
ReactJS UNSAFE_componentWillReceiveProps() Method
The componentWillReceiveProps() is invoked before our mounted React component receives new props. It is called during the updating phase of the React Life-cycle. It is used to update the state in response to some changes in our props. We can't call this with initial props during mounting because React calls this method only when our component's pro
3 min read
ReactJS shouldComponentUpdate() Method
The shouldComponentUpdate method allows us to exit the complex react update life cycle to avoid calling it again and again on every re-render. It only updates the component if the props passed to it changes. The shouldComponentUpdate method is majorly used for optimizing the performance and to increase the responsiveness of the website but do not r
3 min read
Article Tags :