Skip to content

Instantly share code, notes, and snippets.

@hedgerh
Forked from sebmarkbage/Enhance.js
Last active August 27, 2015 18:24
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 hedgerh/6dc2ad8ee93ef81bbf9c to your computer and use it in GitHub Desktop.
Save hedgerh/6dc2ad8ee93ef81bbf9c to your computer and use it in GitHub Desktop.
Higher-order Components
import { Component } from "React";
export var Enhance = (ComposedComponent, ajaxCall) => class extends Component {
constructor() {
this.state = { data: null };
}
componentDidMount() {
ajaxCall()
.then(function(data) {
this.setState({ data: data });
});
}
render() {
return <ComposedComponent {...this.props} data={this.state.data} />;
}
};
import { Enhance } from "./Enhance";
var API_URL = 'abc.com';
function request1() {
return request.get(API_URL + '/someData');
}
function request2() {
return request.get(API_URL + '/someOtherData');
}
class MyComponent {
render() {
if (!this.data) return <div>Waiting...</div>;
return <div>{this.data}</div>;
}
}
export const SituationOne = Enhance(MyComponent, request1); // Enhanced component
export const SituationTwo = Enhance(MyComponent, request2); // Enhanced component
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment