Skip to content

Instantly share code, notes, and snippets.

@jeromepl
Forked from sebmarkbage/Enhance.js
Last active April 6, 2020 20:26
Show Gist options
  • Save jeromepl/2f7df563f273563261690221c22aa0af to your computer and use it in GitHub Desktop.
Save jeromepl/2f7df563f273563261690221c22aa0af to your computer and use it in GitHub Desktop.
Higher-order Components in React with ES7
import { Component } from "React";
export default (ComposedComponent) => class extends Component {
constructor(props) {
super(props);
this.state = { data: null };
}
componentDidMount() {
this.setState({ data: 'Hello' });
}
render() {
return <ComposedComponent {...this.props} data={this.state.data} />;
}
};
import { Component } from "React";
import enhance from "./Enhance";
@enhance
export default class MyComponent extends Component {
render() {
if (!this.props.data) return <div>Waiting...</div>;
return <div>{this.props.data}</div>;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment