Mastering React’s New Lifecycle Methods: A Complete Guide

This article explains how React 16 introduced new lifecycle methods, phased component lifecycles, deprecation of old methods with UNSAFE_ prefixes, error handling, and provides practical examples and best‑practice recommendations for building robust React components.

21CTO
21CTO
21CTO
Mastering React’s New Lifecycle Methods: A Complete Guide

Introduction

Since React v16, performance and new APIs have become prominent, and the lifecycle has been revised to support async rendering and avoid misuse.

New Lifecycle Overview

React 16.3 added two new lifecycle methods:

getDerivedStateFromProps()

getSnapshotBeforeUpdate()

Older lifecycle methods are gradually deprecated but remain with UNSAFE_ prefixes for backward compatibility.

In v16.3 the three legacy methods (componentWillMount, componentWillReceiveProps, componentWillUpdate) are not removed; they are renamed with an UNSAFE_ prefix. Warnings appear in v16.4 and the methods are removed in v17.

Lifecycle Phases

Mounting

constructor()

static getDerivedStateFromProps()

componentWillMount()/UNSAFE_componentWillMount()

render()

componentDidMount()

The constructor initializes state and binds event handlers. If getDerivedStateFromProps is defined, componentWillMount is skipped.

Updating

componentWillReceiveProps()/UNSAFE_componentWillReceiveProps()

static getDerivedStateFromProps()

shouldComponentUpdate()

componentWillUpdate()/UNSAFE_componentWillUpdate()

render()

getSnapshotBeforeUpdate()

componentDidUpdate()

When getDerivedStateFromProps or getSnapshotBeforeUpdate is present, the unsafe methods are not called.

shouldComponentUpdate returns true or false to control re‑rendering; PureComponent implements a shallow comparison of props and state.

Unmounting

componentWillUnmount()

Use this phase to clean up timers, event listeners, and network requests. setState must not be called here.

Error Handling

componentDidCatch(err, info)

This method catches errors during rendering, in lifecycle methods, and in constructors of child components, but not in event handlers, async code, server‑side rendering, or errors thrown by the error boundary itself.

Code Examples

class ExampleComponent extends React.Component {
  constructor(props) {
    super(props);
    this.state = { isScrollingDown: false, lastRow: null };
  }

  static getDerivedStateFromProps(nextProps, prevState) {
    if (nextProps.currentRow !== prevState.lastRow) {
      return { isScrollingDown: nextProps.currentRow > prevState.lastRow };
    }
    return null;
  }

  componentDidMount() {
    // async requests, event bindings, etc.
  }

  render() {
    return <div>{/* component UI */}</div>;
  }
}
class ScrollingList extends React.Component {
  constructor(props) {
    super(props);
    this.listRef = React.createRef();
  }

  getSnapshotBeforeUpdate(prevProps, prevState) {
    if (prevProps.list.length < this.props.list.length) {
      const list = this.listRef.current;
      return list.scrollHeight - list.scrollTop;
    }
    return null;
  }

  componentDidUpdate(prevProps, prevState, snapshot) {
    if (snapshot !== null) {
      const list = this.listRef.current;
      list.scrollTop = list.scrollHeight - snapshot;
    }
  }

  render() {
    return (
      <div ref={this.listRef}>
        {/* list items */}
      </div>
    );
  }
}

Conclusion

React’s lifecycle can be visualized at react‑lifecycle‑methods‑diagram . While backward compatibility exists, it is recommended to avoid deprecated methods and adopt the new lifecycle for future‑proof components. For older React versions, the react‑lifecycles‑compat polyfill provides the new methods.

Old lifecycle diagram
Old lifecycle diagram
New lifecycle diagram
New lifecycle diagram
Lifecycle summary
Lifecycle summary
Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

ComponentLifecycleError Handling
21CTO
Written by

21CTO

21CTO (21CTO.com) offers developers community, training, and services, making it your go‑to learning and service platform.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.