What is Life Cycle method in React?

Chief Editor
4 Min Read

In React, lifecycle methods are special methods that are automatically called at different stages of a component’s life cycle.
They allow you to control what happens when a component is created, updated, or destroyed.


Phases of React Component Lifecycle:

React class components have three main lifecycle phases:

PhaseDescription
MountingWhen the component is created and added to the DOM.
UpdatingWhen the component is re-rendered due to state or prop changes.
UnmountingWhen the component is removed from the DOM.

Lifecycle Methods in Class Components:

1. Mounting Phase (Component is Created)

MethodDescription
constructor()Initialize state and bind methods.
static getDerivedStateFromProps()Sync state with props before render (rarely used).
render()Render JSX to the DOM.
componentDidMount()Called after the component is rendered to the DOM → Ideal for API calls.

2. Updating Phase (Component Re-renders)

MethodDescription
static getDerivedStateFromProps()Sync state with props when props change (rarely used).
shouldComponentUpdate()Control if component should re-render (optimize performance).
render()Renders JSX again.
getSnapshotBeforeUpdate()Capture DOM state before the update (e.g., scroll position).
componentDidUpdate()Called after the component re-renders → Ideal for side effects after updates.

3. Unmounting Phase (Component is Removed)

MethodDescription
componentWillUnmount()Called before the component is removed from the DOM → Clean up tasks like timers, event listeners, API calls, etc.

Summary Table (Lifecycle Methods Overview):

PhaseMethodPurpose
Mountingconstructor()Initialize state, bind event handlers.
getDerivedStateFromProps()Sync state from props before rendering.
render()Render the component.
componentDidMount()Perform side effects like data fetching, subscriptions.
UpdatinggetDerivedStateFromProps()Sync state from props before update.
shouldComponentUpdate()Optimize rendering (e.g., prevent unnecessary re-renders).
render()Re-render component.
getSnapshotBeforeUpdate()Capture DOM information before update.
componentDidUpdate()Perform side effects after component updates.
UnmountingcomponentWillUnmount()Clean up side effects (e.g., cancel subscriptions, timers).

React Lifecycle Diagram (Visual Summary):

luaCopyEditMOUNTING
|-- constructor()
|-- getDerivedStateFromProps()
|-- render()
|-- componentDidMount()

UPDATING (state/props change)
|-- getDerivedStateFromProps()
|-- shouldComponentUpdate()
|-- render()
|-- getSnapshotBeforeUpdate()
|-- componentDidUpdate()

UNMOUNTING
|-- componentWillUnmount()

Lifecycle Methods in Functional Components (React Hooks Approach):

With Hooks, we use:

  • useEffect() – Handles side effects (combines componentDidMount, componentDidUpdate, componentWillUnmount).
  • useState() – Manages state.
  • useMemo(), useCallback(), etc. – Optimize performance.

Example:

javascriptCopyEditimport { useState, useEffect } from 'react';

function App() {
  const [count, setCount] = useState(0);

  // ComponentDidMount + ComponentDidUpdate
  useEffect(() => {
    console.log('Component rendered or updated');
  });

  // ComponentDidMount (runs only once)
  useEffect(() => {
    console.log('Component mounted');
  }, []);

  // ComponentWillUnmount
  useEffect(() => {
    return () => {
      console.log('Component unmounted');
    };
  }, []);

  return (
    <div>
      <p>Count: {count}</p>
      <button onClick={() => setCount(count + 1)}>Increase</button>
    </div>
  );
}

Key Takeaways:

FeatureDescription
Lifecycle MethodsSpecial methods in class components to manage component behavior during mounting, updating, and unmounting phases.
Class vs FunctionalClass Components: Use lifecycle methods.
Functional Components: Use Hooks (useEffect).
Hooks Replacing LifecycleuseEffect() combines componentDidMount, componentDidUpdate, componentWillUnmount.

When to Use Lifecycle Methods (Hooks):

✅ Fetching Data → componentDidMount() / useEffect().
✅ Clean Up Subscriptions, Timers → componentWillUnmount() / useEffect() with a cleanup function.
✅ DOM Updates after Render → componentDidUpdate() / useEffect().

Modern React development primarily uses functional components with hooks, but understanding lifecycle methods is still valuable, especially when working with older class-based codebases.

Share This Article
Leave a Comment