Skip to content

Instantly share code, notes, and snippets.

@parwatcodes
Created April 2, 2019 18:34
Show Gist options
  • Save parwatcodes/926f8d69c1e859e37b757d80fe51a906 to your computer and use it in GitHub Desktop.
Save parwatcodes/926f8d69c1e859e37b757d80fe51a906 to your computer and use it in GitHub Desktop.
a example for higher order component
// presentational component
const Greeting = ({ name }) => {
if (!name) {
return <div>Connecting...</div>;
}
return <div>Hi {name}!</div>;
};
// hoc
const Connect = ComposedComponent =>
class extends React.Component {
constructor() {
super();
this.state = { name: "" };
}
componentDidMount() {
// this would fetch or connect to a store
this.setState({ name: "Michael" });
}
render() {
return <ComposedComponent {...this.props} name={this.state.name} />;
}
};
const ConnectedMyComponent = Connect(Greeting);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment