Showing posts with label componentDidMount. Show all posts
Showing posts with label componentDidMount. Show all posts

Sunday, August 12, 2018

ComponentDidMount example

The componentDidMount life-cycle method is the last lifecycle method executed in the component mounting phase. componentDidMount() is invoked immediately after the component is mounted.

The following example uses ComponentDidMount() to alter the state property "title", in this example we will not make any api calls, but will use setTimeout() to inject a 1 second delay in setting the state. The state change will re-trigger render() and the updated state will be displayed in the browser.

React Component Lifecycle - componentDidMount

The componentDidMount life-cycle method is the last life cycle method executed in the component mounting phase. componentDidMount() is invoked immediately after the component is mounted.

Any initialization to the component which requires the DOM nodes should be done in this method. In this method we can perform activities which require DOM layout like measuring DOM element size etc. We can also alter the state based on the DOM element / layout.