Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@GregoryCollett
Created March 14, 2017 10:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save GregoryCollett/f19bc12de35b56d04668ac22c8481843 to your computer and use it in GitHub Desktop.
Save GregoryCollett/f19bc12de35b56d04668ac22c8481843 to your computer and use it in GitHub Desktop.
import React, { Component } from 'react';
export const withLifecycle = (hooks) => (BaseComponent) => {
return class WithLifecycle extends Component {
componentWillMount() {
hooks.willMount && hooks.willMount(this.props);
}
componentDidMount() {
hooks.didMount && hooks.didMount(this.props);
}
componentWillReceiveProps(props) {
hooks.willReceiveProps && hooks.willReceiveProps(props);
}
shouldComponentUpdate(props) {
if (hooks.shouldUpdate) {
return hooks.shouldUpdate(this.props, props);
}
return true;
}
componentWillUpdate(props, state) {
hooks.willUpdate && hooks.willUpdate(props, state);
}
componentDidUpdate(props, state) {
hooks.didUpdate && hooks.didUpdate(props, state);
}
componentWillUnmount() {
hooks.willUnmount && hooks.willUnmount(this.props);
}
componentWillReceiveProps(props) {
hooks.willReceiveProps && hooks.willReceiveProps(this.props, props);
}
render() {
return (
<BaseComponent
{...this.props}
/>
);
}
};
};
export default withLifecycle;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment