-
-
Save yangshun/c90663760eb1cd93d23a to your computer and use it in GitHub Desktop.
Higher-order Components
This file contains hidden or 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
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} />; | |
} | |
}; |
This file contains hidden or 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
import { Enhance } from "./Enhance"; | |
import { Component } from "React"; | |
class MyComponent extends Component { | |
render() { | |
if (!this.props.data) return <div>Waiting...</div>; | |
return <div>{this.props.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