
Security News
Deno 2.4 Brings Back deno bundle, Improves Dependency Management and Observability
Deno 2.4 brings back bundling, improves dependency updates and telemetry, and makes the runtime more practical for real-world JavaScript projects.
react-pure-lifecycle
Advanced tools
Add lifecycle methods to your functional components with purity
$ npm i react-pure-lifecycle --save
The primary use case for this decorator is with functional components, however you can use it with Component
and PureComponent
classes as well.
import React from 'react';
import lifecycle from 'react-pure-lifecycle';
// create your lifecycle methods
const componentDidMount = (props) => {
console.log('I mounted! Here are my props: ', props);
};
// make them properties on a standard object
const methods = {
componentDidMount
};
const FunctionalComponent = ({children}) => {
return (
<div>
{children}
</div>
);
};
// decorate the component
export default lifecycle(methods)(FunctionalComponent);
The complete list of lifecycle methods are supported, minus constructor
(if you want to fire something as early as possible, use componentWillMount
). The first parameter passed to each lifecycle method is the component's current props
, and then all standard parameters for that given lifecycle method follow. For a detailed explanation of each of the methods and the parameters they expect, check the React documentation.
You can also add options to customize the use of the decorator, see Options for more details.
import React, {
Component
} from 'react';
import lifecycle from 'react-pure-lifecycle';
const componentDidUpdate = (props, previousProps) => {
console.log('I updated! Here are my current and previous props: ', props, previousProps);
};
const methods = {
componentDidUpdate
};
@lifecycle(methods)
class ComponentClass extends Component {
render() {
const {
children
} = this.props;
return (
<div>
{children}
</div>
);
}
}
Not a whole lot of gain here other than the fact that you now have a pure function that you can test independently (no need to create an instance). This decoration method will also work on PureComponent
s.
Each lifecycle method is also provided as their own decorator, if you just want to bind a single method (receives the method itself instead of an object of methods):
import React from 'react';
import {
shouldComponentUpdate
} from 'react-pure-lifecycle';
const onlyUpdateIfChanged = (props, nextProps) => {
return props.children !== nextProps.children;
};
const FunctionalComponent = ({children}) => {
return (
<div>
{children}
</div>
);
};
// decorate the component
export default shouldComponentUpdate(onlyUpdateIfChanged)(FunctionalComponent);
If you want to provide options to these specific method decorators, you can pass them as the second argument.
In addition to providing the standard lifecycle methods, starting in 2.x.x
you can add child context to functional components (something normally React
disallows). Example:
import React from 'react';
import PropTypes from 'prop-types';
import lifecycle from 'react-pure-lifecycle';
const methods = {
getChildContext(props) {
return {
foo: 'bar'
};
}
};
const Foo = () => {
return (
<div>
Hello!
</div>
);
};
Foo.childContextTypes = {
foo: PropTypes.string.isRequired
};
export default lifecycle(methods, options)(Foo);
Like standard lifecycle methods, this will also apply to components instantiated via the class method, with the only difference from standard behavior being the injection of the props
argument.
Additional options can be passed as the second parameter to the decorator, with the following shape:
{
injectProps: boolean, // should the component's props be injected as first argument (default: true)
usePureComponent: boolean // should the component rendered be a PureComponent (default: true)
}
By default, all lifecycle methods will receive the component's current props
as the first argument, and then all standard arguments for the given lifecycle method following. If you would like to disable this injection, set injectProps
to false
:
import React from 'react';
import lifecycle from 'react-pure-lifecycle';
const methods = {
componentDidUpdate(previousProps) {
console.log('Normally the first argument would be the currentProps,' previousProps);
}
};
const options = {
injectProps: false
};
const Foo = () => {
return (
<div>
Hello!
</div>
);
};
export default lifecycle(methods, options)(Foo);
By default, functional components that have the decorator applied will use PureComponent
as the foundation of the HOC, allowing for the same render-limiting performance optimizations that a PureComponent
class has. If you would like to disable this and instead use the standard Component
class for the HOC, set usePureComponent
to false
.
import React from 'react';
import lifecycle from 'react-pure-lifecycle';
const methods = {
componentDidMount(props) {
console.log('Mounted with props: ', props)
}
};
const options = {
usePureComponent: false
};
const Foo = () => {
return (
<div>
Hello!
</div>
);
};
export default lifecycle(methods, options)(Foo);
Please note that this option will only affect functional components; if you apply the decorator to a standard component class, it will use the same component class that was decorated.
Standard stuff, clone the repo and npm install
dependencies. The npm scripts available:
build
=> run webpack to build unminified JS with NODE_ENV
set to development
and source mapbuild:minifed
=> run webpack to build minified JS with NODE_ENV
set to production
clean
=> run rimraf
on both lib
and dist
lint
=> run ESLint against all files in the src
folderprepublish
=> runs prepubish:compile
prepublish:compile
=> run clean
, lint
, test
, transpile
, build
, and build-minified
start
=> run webpack dev server to run example app (playground!)test
=> run AVA test functions with NODE_ENV=test
test:coverage
=> run test
with nyc to get output of code coveragetest:watch
=> run test
, but with persistent watchertranspile
=> run babel against all files in src
to create files in lib
3.0.0
UNSAFE_componentWillMount
getChildContext
UNSAFE_componentWillReceiveProps
UNSAFE_componentWillUpdate
getSnapshotBeforeUpdate
componentDidCatch
require('react-pure-lifecycle').default
FAQs
Add pure function lifecycle methods to any React component
The npm package react-pure-lifecycle receives a total of 399 weekly downloads. As such, react-pure-lifecycle popularity was classified as not popular.
We found that react-pure-lifecycle demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer 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
Deno 2.4 brings back bundling, improves dependency updates and telemetry, and makes the runtime more practical for real-world JavaScript projects.
Security News
CVEForecast.org uses machine learning to project a record-breaking surge in vulnerability disclosures in 2025.
Security News
Browserslist-rs now uses static data to reduce binary size by over 1MB, improving memory use and performance for Rust-based frontend tools.