Skip to content

Instantly share code, notes, and snippets.

@stephenjwatkins
Created August 9, 2018 20:11
Show Gist options
  • Save stephenjwatkins/a2babff338ff22d27e57896653617867 to your computer and use it in GitHub Desktop.
Save stephenjwatkins/a2babff338ff22d27e57896653617867 to your computer and use it in GitHub Desktop.
Generic Lifecycles component for React.
import React from "react";
import isFunction from "lodash/isFunction";
class Lifecycles extends React.Component {
async componentDidMount() {
if (this.props.didMount) {
await this.props.didMount({ props: this.props });
}
}
async componentDidUpdate(prevProps) {
if (this.props.didUpdate) {
await this.props.didUpdate({ props: this.props, prevProps });
}
}
componentWillUnmount() {
if (this.props.willUnmount) {
this.props.willUnmount({ props: this.props });
}
}
render() {
if (isFunction(this.props.children)) {
return this.props.children();
}
if (this.props.children) {
return this.props.children;
}
return null;
}
}
export { Lifecycles };
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment