Skip to content

Instantly share code, notes, and snippets.

@sathify
Last active October 25, 2015 07:05
Show Gist options
  • Select an option

  • Save sathify/767c06aaee83cad9a1d0 to your computer and use it in GitHub Desktop.

Select an option

Save sathify/767c06aaee83cad9a1d0 to your computer and use it in GitHub Desktop.
Higher Order Components React
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} />;
}
}
};
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
@sathify
Copy link
Author

sathify commented Oct 25, 2015

export default TripleEnhance(DoubleEnhance(Enhance(MyComponent)));

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment