Registry / react-lifecycles-compat

react-lifecycles-compat

JSON →
library3.0.4jsnpmunverified

`react-lifecycles-compat` is a JavaScript polyfill designed to provide backwards compatibility for React class components. It enables the use of the `static getDerivedStateFromProps` and `getSnapshotBeforeUpdate` lifecycles (introduced in React 16.3) with older versions of React, specifically 0.14.9+. This functionality was critical for libraries and applications during the transition period when React deprecated `componentWillMount`, `componentWillReceiveProps`, and `componentWillUpdate` due to issues with async rendering. By using this polyfill, developers could adopt the newer, safer lifecycles without forcing users of older React versions to upgrade their entire React setup, thus facilitating a smoother ecosystem transition. The package is currently at version 3.0.4. While still functional, its primary relevance has diminished significantly with the widespread adoption of modern React versions (17+) and the prevalence of React Hooks.

npm install react-lifecycles-compat
INSTALL
IMPORT
SIG · REACT-LIFECYCLES-C
R
react-lifecycles-compat
javascriptv3.0.4
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

polyfill
import { polyfill } from 'react-lifecycles-compat';
const { polyfill } = require('react-lifecycles-compat');
Primarily used for applying the polyfill to a class component.

Demonstrates applying the `polyfill` to a React class component using `getDerivedStateFromProps` and `getSnapshotBeforeUpdate`.

import React from 'react'; import { polyfill } from 'react-lifecycles-compat'; class ExampleComponent extends React.Component { static getDerivedStateFromProps(nextProps, prevState) { // This method works with React 16.3+ natively, // and with older React versions (0.14.9+) after polyfilling. if (nextProps.value !== prevState.internalValue) { return { internalValue: nextProps.value }; } return null; } constructor(props) { super(props); this.state = { internalValue: props.value, scrollPos: 0 }; this.listRef = React.createRef(); } getSnapshotBeforeUpdate(prevProps, prevState) { // Captures scroll position before DOM update if (this.listRef.current) { return this.listRef.current.scrollTop; } return null; } componentDidUpdate(prevProps, prevState, snapshot) { // Use snapshot to restore scroll position if (snapshot !== null && this.listRef.current) { this.listRef.current.scrollTop = snapshot; } } render() { return ( <div ref={this.listRef} style={{ height: '200px', overflow: 'auto' }}> <h1>Value: {this.state.internalValue}</h1> <p>Scrollable content...</p> </div> ); } } // Apply the polyfill to make new lifecycles work with older React versions polyfill(ExampleComponent); export default ExampleComponent;
Debug
Known issues
breakingWhen using `react-lifecycles-compat`, your component must NOT define the deprecated lifecycles (`componentWillMount`, `componentWillReceiveProps`, `componentWillUpdate`). Defining them alongside new lifecycles will throw an error.
fix
Remove all instances of `componentWillMount`, `componentWillReceiveProps`, and `componentWillUpdate` from components you are polyfilling.
affects: >=1.0.0
gotchaIf a component defines `getSnapshotBeforeUpdate`, it must also define `componentDidUpdate`. The `getSnapshotBeforeUpdate` lifecycle method's return value is passed as the third argument to `componentDidUpdate`, making them a pair.
fix
Ensure `componentDidUpdate` is present in any component that uses `getSnapshotBeforeUpdate`.
affects: >=1.0.0
deprecatedThis polyfill is primarily relevant for projects needing to support older React versions (pre-16.3, or during the transition to 16.3+). For applications exclusively targeting modern React (17+), its utility is minimal, and direct use of new lifecycles or Hooks is standard.
fix
For new development or when upgrading to React 17+, prefer native `getDerivedStateFromProps` and `getSnapshotBeforeUpdate` or migrate to functional components with Hooks where applicable.
affects: <17.0.0
Errors
Common errors & fixes
Invariant Violation: This component defines getDerivedStateFromProps and componentWillReceiveProps, which is not supported. Please remove componentWillReceiveProps.
A component intended to be polyfilled defines both a new lifecycle (`getDerivedStateFromProps`) and a deprecated one (`componentWillReceiveProps`).
fix
Remove the deprecated `componentWillReceiveProps` method. Similarly, check for `componentWillMount` and `componentWillUpdate`.
Invariant Violation: This component defines getSnapshotBeforeUpdate without also defining componentDidUpdate. getSnapshotBeforeUpdate lifecycle requires a componentDidUpdate lifecycle to be present.
The `getSnapshotBeforeUpdate` lifecycle method was defined without its required counterpart, `componentDidUpdate`.
fix
Add a `componentDidUpdate(prevProps, prevState, snapshot)` method to the component. The `snapshot` argument will receive the value returned by `getSnapshotBeforeUpdate`.
Upgrade
Version history
3.0.4latest on npm
Audit
Dependencies
reactrequiredRuntime dependency for class components and lifecycles.
Agent activity
4 hits · last 30 days
node
4
Resources
react-lifecycles-compat — npm install react-lifecycles-compat · libregistry