Skip to content

Instantly share code, notes, and snippets.

@ryerh
Forked from sebmarkbage/Enhance.js
Created July 14, 2016 03:31
Show Gist options
  • Save ryerh/7b46a1ee0d70e60e27d2c9a8dc3e5a87 to your computer and use it in GitHub Desktop.
Save ryerh/7b46a1ee0d70e60e27d2c9a8dc3e5a87 to your computer and use it in GitHub Desktop.
Higher-order Components
import { Component } from "React";
export var Enhance = ComposedComponent => class extends Component {
constructor() {
this.state = { data: null };
}
componentDidMount() {
this.setState({ data: 'Hello' });
}
render() {
return <ComposedComponent {...this.props} data={this.state.data} />;
}
};
import { Enhance } from "./Enhance";
class MyComponent {
render() {
if (!this.data) return <div>Waiting...</div>;
return <div>{this.data}</div>;
}
}
export default Enhance(MyComponent); // Enhanced component
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment