Execution order of React life cycle methods

NISHANK KUMAR
2 min readMay 19, 2023

The execution order of React lifecycle methods can be grouped into three main phases: mounting, updating, and unmounting. Here’s the general order in which the lifecycle methods are invoked:

Mounting Phase:

  1. constructor(props)
  2. static getDerivedStateFromProps(props, state)
  3. render()
  4. componentDidMount()

Updating Phase:

  1. static getDerivedStateFromProps(props, state)
  2. shouldComponentUpdate(nextProps, nextState)
  3. render()
  4. getSnapshotBeforeUpdate(prevProps, prevState)
  5. componentDidUpdate(prevProps, prevState, snapshot)

Unmounting Phase:

  1. componentWillUnmount()

Now, let’s take a closer look at each phase and the corresponding lifecycle methods:

Mounting Phase:

  1. constructor(props): This is the first method called when an instance of a component is created. It is used to initialize state and bind event handlers.
  2. static getDerivedStateFromProps(props, state): This method is invoked right before rendering and allows updating the state based on changes in props. It is rarely used and often avoided in favor of other approaches.
  3. render(): This method is responsible for rendering the component's UI based on its props and state. It returns JSX or null.
  4. componentDidMount(): This method is called after the component is mounted to the DOM. It is often used for side effects such as fetching data from an API or subscribing to events.

Updating Phase:

  1. static getDerivedStateFromProps(props, state): Same as in the mounting phase, this method is called before rendering to update the state based on changes in props.
  2. shouldComponentUpdate(nextProps, nextState): This method allows you to control whether the component should re-render based on changes in props or state. It can be used to optimize performance by preventing unnecessary re-renders.
  3. render(): Same as in the mounting phase, this method re-renders the updated UI based on props and state.
  4. getSnapshotBeforeUpdate(prevProps, prevState): This method is called immediately before changes from the component are reflected in the DOM. It allows capturing information from the DOM before it is potentially changed.
  5. componentDidUpdate(prevProps, prevState, snapshot): This method is called after the component has been updated and the changes are reflected in the DOM. It is often used for side effects or updating the state based on the new props or state.

Unmounting Phase:

  1. componentWillUnmount(): This method is called just before the component is unmounted from the DOM. It is used for cleanup tasks such as canceling timers, removing event listeners, or cleaning up subscriptions.

It’s worth noting that with the introduction of React Hooks, the traditional lifecycle methods (componentDidMount, componentDidUpdate, componentWillUnmount, etc.) are still supported, but the recommended approach is to use the useEffect hook instead.

Remember that not all components will have all lifecycle methods, as some are optional and can be omitted if not needed. Also, the deprecated lifecycle methods (componentWillMount, componentWillReceiveProps, componentWillUpdate) should be avoided in new code and replaced with alternatives.

--

--