Last active
October 25, 2015 07:05
-
-
Save sathify/767c06aaee83cad9a1d0 to your computer and use it in GitHub Desktop.
Higher Order Components 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 { Component } from "React"; | |
| export default function Enhance(ComposedComponent) { | |
| return class extends Component { | |
| constructor() { | |
| this.state = { time: null }; | |
| } | |
| componentDidMount() { | |
| this.setState({ time: new Date().now() }); | |
| } | |
| render() { | |
| return <ComposedComponent {...this.props} time={this.state.time} />; | |
| } | |
| } | |
| }; |
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 { Enhance } from "./Enhance"; | |
| class MyComponent { | |
| render() { | |
| if (!this.time) return <div>Waiting...</div>; | |
| return <div>{this.time}</div>; | |
| } | |
| } | |
| export default Enhance(MyComponent); // Enhanced component |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
export default TripleEnhance(DoubleEnhance(Enhance(MyComponent)));