Created
August 9, 2018 20:11
-
-
Save stephenjwatkins/a2babff338ff22d27e57896653617867 to your computer and use it in GitHub Desktop.
Generic Lifecycles component for React.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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