Socket
Socket
Sign inDemoInstall

react-lifecycles-compat

Package Overview
Dependencies
0
Maintainers
7
Versions
17
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    react-lifecycles-compat

Backwards compatibility polyfill for React class components


Version published
Weekly downloads
5M
decreased by-20.37%
Maintainers
7
Install size
29.4 kB
Created
Weekly downloads
 

Package description

What is react-lifecycles-compat?

The react-lifecycles-compat npm package is designed to polyfill the new React lifecycle methods to work with older versions of React (versions 0.14.9 to 16.2). This allows developers to use new features of React like getDerivedStateFromProps and getSnapshotBeforeUpdate in their existing projects without needing to completely refactor their components for newer React versions.

What are react-lifecycles-compat's main functionalities?

Polyfill for getDerivedStateFromProps

This feature allows the use of getDerivedStateFromProps in components that are part of projects using older versions of React. The polyfill function modifies the component to use this lifecycle method correctly even if the React version originally does not support it.

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

class MyComponent extends React.Component {
  static getDerivedStateFromProps(nextProps, prevState) {
    if (nextProps.value !== prevState.value) {
      return {
        value: nextProps.value
      };
    }
    return null;
  }
}

polyfill(MyComponent);
export default MyComponent;

Polyfill for getSnapshotBeforeUpdate

This feature enables the use of getSnapshotBeforeUpdate, which is useful for capturing some information from the DOM before it is potentially changed due to updates. The polyfill ensures it can be used in older React versions.

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

class MyComponent extends React.Component {
  getSnapshotBeforeUpdate(prevProps, prevState) {
    if (prevProps.list.length < this.props.list.length) {
      const listDiff = this.props.list.length - prevProps.list.length;
      return { listDiff: listDiff };
    }
    return null;
  }
  componentDidUpdate(prevProps, prevState, snapshot) {
    if (snapshot !== null) {
      console.log('List length increased by', snapshot.listDiff);
    }
  }
}

polyfill(MyComponent);
export default MyComponent;

Other packages similar to react-lifecycles-compat

Changelog

Source

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)

Readme

Source

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.

FAQs

Last updated on 11 May 2018

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc