Skip to content

Instantly share code, notes, and snippets.

@sebmarkbage
Last active November 7, 2024 13:05
Show Gist options
  • Save sebmarkbage/ef0bf1f338a7182b6775 to your computer and use it in GitHub Desktop.
Save sebmarkbage/ef0bf1f338a7182b6775 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
@ivenxu
Copy link

ivenxu commented Nov 24, 2018

Not quite see the difference between HoC and old decorator pattern. I would say HoC is a nice react implementation of decorator pattern.

@dexygen
Copy link

dexygen commented Jan 14, 2019

Your constructor needs super(props) as the first line

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