React Lifecycle

In React, a component’s lifecycle refers to the sequence of events that occurs from the time the component is created until it is destroyed.

A component’s lifecycle can be divided into three main phases:

  1. Mounting: This is the phase where a component is being inserted into the DOM. During this phase, the component’s constructor method is called, followed by the render() method. After the component is rendered, the componentDidMount() method is called.
  2. Updating: This is the phase where a component is being re-rendered as a result of a change in its props or state. During this phase, the shouldComponentUpdate() method is called, followed by the render() method. After the component is re-rendered, the componentDidUpdate() method is called.
  3. Unmounting: This is the phase where a component is being removed from the DOM. During this phase, the componentWillUnmount() method is called.

There are several methods available in the component lifecycle that you can use to perform specific tasks at different points in a component’s lifecycle. For example, you can use the componentDidMount() method to make an API call when a component is mounted, or you can use the shouldComponentUpdate() method to optimize performance by avoiding unnecessary re-renders.

It’s important to note that not all lifecycle methods are applicable to every component, and you should choose the appropriate methods for your specific use case.

Here are some examples of each of the main lifecycle methods in React:

  1. Mounting:
  • constructor(): This is called before a component is mounted. You can use the constructor to initialize state and bind methods.
constructor(props) {
  super(props);
  this.state = {
    count: 0
  };
  this.handleClick = this.handleClick.bind(this);
}
  • render(): This is called to render the component. It must return a valid React element.
render() {
  return (
    <div>
      <p>You clicked {this.state.count} times</p>
      <button onClick={this.handleClick}>Click me</button>
    </div>
  );
}
  • componentDidMount(): This is called after the component is mounted. You can use this method to trigger an action after the component is rendered, such as making an API call.
componentDidMount() {
  fetch('/data.json')
    .then(response => response.json())
    .then(data => this.setState({ data }));
}
  1. Updating:
  • shouldComponentUpdate(nextProps, nextState): This is called before a component is re-rendered as a result of a change in props or state. You can use this method to optimize performance by avoiding unnecessary re-renders. It should return a boolean value indicating whether the component should update.
shouldComponentUpdate(nextProps, nextState) {
  return this.state.count !== nextState.count;
}

render(): This is called to re-render the component. It must return a valid React element.

render() {
  return (
    <div>
      <p>You clicked {this.state.count} times</p>
      <button onClick={this.handleClick}>Click me</button>
    </div>
  );
}
  • componentDidUpdate(prevProps, prevState): This is called after the component is updated. You can use this method to trigger an action after the component has been updated, such as making an API call.
componentDidUpdate(prevProps, prevState) {
  if (prevState.count !== this.state.count) {
    fetch(`/data/${this.state.count}.json`)
      .then(response => response.json())
      .then(data => this.setState({ data }));
  }
}
  1. Unmounting:
  • componentWillUnmount(): This is called before a component is unmounted and destroyed. You can use this method to perform cleanup tasks, such as canceling network requests or removing event listeners.
componentWillUnmount() {
  this.serverRequest.abort();
}

What is the use of life-cycle-methods in React ?

Lifecycle methods in React are functions that are called at different points in a component’s lifecycle, from the time it is created until it is destroyed. These methods allow you to perform specific tasks at each stage of a component’s lifecycle, such as initializing state, making an API call, or triggering an action after a component has been updated.

Lifecycle methods are useful because they give you control over a component’s behavior at different points in its lifecycle. For example, you can use the componentDidMount()' method to make an API call when a component is mounted, or you can use the shouldComponentUpdate()' method to optimize performance by avoiding unnecessary re-renders.

Lifecycle methods are also useful for performing cleanup tasks, such as canceling network requests or removing event listeners. This can help prevent memory leaks and improve the overall performance of your application.

Overall, lifecycle methods are an important part of building applications with React, and they give you a lot of control over how your components behave and interact with the rest of your application.