Skip to content

Instantly share code, notes, and snippets.

@kevmo
Forked from sebmarkbage/Enhance.js
Last active September 7, 2017 15:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kevmo/6ea97048ed3993931b787e2d57526faf to your computer and use it in GitHub Desktop.
Save kevmo/6ea97048ed3993931b787e2d57526faf to your computer and use it in GitHub Desktop.
Higher-order Components

A higher-order component is just a function that takes an existing component and returns another component that wraps it.

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