Security News
New Proposed CISA Mandate Would Require Critical Infrastructure to Report Ransom Payments Within 24 Hours
CISA has proposed a set of new rules that would require critical infrastructure to report cyber incidents and ransom payments.
react-lifecycles-compat
Advanced tools
Backwards compatibility polyfill for React class components
Weekly downloads
Readme
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.
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;
Currently, this polyfill supports static getDerivedStateFromProps
and getSnapshotBeforeUpdate
- both introduced in version 16.3.
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
Backwards compatibility polyfill for React class components
The npm package react-lifecycles-compat receives a total of 4,935,573 weekly downloads. As such, react-lifecycles-compat popularity was classified as popular.
We found that react-lifecycles-compat demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 7 open source maintainers collaborating on the project.
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.
Security News
CISA has proposed a set of new rules that would require critical infrastructure to report cyber incidents and ransom payments.
Security News
Redis is no longer OSS, breaking its explicit commitment to remain under the BSD 3-Clause License forever. This has angered contributors who are now working to fork the software.
Product
Socket AI now enables 'AI detected potential malware' alerts by default, ensuring users benefit from AI-powered state-of-the-art malware detection without needing to opt-in.