Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

react-lifecycles-compat

reactjs18.3mMIT3.0.4TypeScript support: definitely-typed

Backwards compatibility polyfill for React class components

readme

react-lifecycles-compat

What is this project?

React version 17 will deprecate several of the class component API lifecycles: componentWillMount, componentWillReceiveProps, and componentWillUpdate. (Read the Update on Async rendering blog post to learn more about why.) A couple of new lifecycles are also being added to better support async rendering mode.

Typically, this type of change would require third party libraries to release a new major version in order to adhere to semver. However, the react-lifecycles-compat polyfill offers a way to use the new lifecycles with older versions of React as well (0.14.9+) so no breaking release is required. This enables shared libraries to support both older and newer versions of React simultaneously.

How can I use the polyfill

First, install the polyfill from NPM:

# Yarn
yarn add react-lifecycles-compat

# NPM
npm install react-lifecycles-compat --save

Next, update your component and replace any of the deprecated lifecycles with new ones introduced with React 16.3. (Refer to the React docs for examples of how to use the new lifecycles.)

Lastly, use the polyfill to make the new lifecycles work with older versions of React:

import React from 'react';
import {polyfill} from 'react-lifecycles-compat';

class ExampleComponent extends React.Component {
  static getDerivedStateFromProps(nextProps, prevState) {
    // Normally this method would only work for React 16.3 and newer,
    // But the polyfill will make it work for older versions also!
  }

  getSnapshotBeforeUpdate(prevProps, prevState) {
    // Normally this method would only work for React 16.3 and newer,
    // But the polyfill will make it work for older versions also!
  }

  // render() and other methods ...
}

// Polyfill your component so the new lifecycles will work with older versions of React:
polyfill(ExampleComponent);

export default ExampleComponent;

Which lifecycles are supported?

Currently, this polyfill supports static getDerivedStateFromProps and getSnapshotBeforeUpdate- both introduced in version 16.3.

Validation

Note that in order for the polyfill to work, none of the following lifecycles can be defined by your component: componentWillMount, componentWillReceiveProps, or componentWillUpdate.

Note also that if your component contains getSnapshotBeforeUpdate, componentDidUpdate must be defined as well.

An error will be thrown if any of the above conditions are not met.

changelog

[Unreleased]

<summary> Changes that have landed in master but are not yet released. Click to see more. </summary>

3.0.4 (May 11, 2018)

Fixed shallow renderer regression (introduced in 3.0.3) that caused setState updater to fail due to incorrect this. (#26)

3.0.3 (May 9, 2018)

Fixed an edge case bug where a batched update containing both a setState updater and a parent re-render could result in dropped state updates. (#24)

3.0.2 (April 11, 2018)

Replaced an unintentional template literal to ensure broader browser compatibility. (ce42fe4)

3.0.1 (April 10, 2018)

Replaced a few unintentional let keywords with var to ensure broader browser compatibility. (#17)

3.0.0 (April 9, 2018)

Throw an error for any polyfilled component that mixes old lifecycles (componentWillMount, componentWillReceiveProps, or componentWillUpdate) and new lifecycles (getDerivedStateFromProps or getSnapshotBeforeUpdate) as React 16.3+ does not support this case and will not invoke the old lifecycles. This error ensures consistent behavior between React 16.3+ and older versions. (#14)

2.0.1 (April 9, 2018)

Add a DEV mode warning for any polyfilled component that mixes old lifecycles (componentWillMount, componentWillReceiveProps, or componentWillUpdate) and new lifecycles (getDerivedStateFromProps or getSnapshotBeforeUpdate) as React 16.3+ does not support this case and will not invoke the old lifecycles. This warning ensures consistent behavior between React 16.3+ and older versions. (#15)

2.0.0 (April 4, 2018)

Package uses a named export and includes an ES6 module build. (#11)

// 1.x (before)
import polyfill from 'react-lifecycles-compat';

// 2.x (after)
import {polyfill} from 'react-lifecycles-compat';

1.1.4 (April 3, 2018)

Improved handling of falsy return values from polyfilled getSnapshotBeforeUpdate() lifecycle. #12