Created
April 2, 2019 18:34
-
-
Save parwatcodes/926f8d69c1e859e37b757d80fe51a906 to your computer and use it in GitHub Desktop.
a example for higher order component
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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