Created
March 26, 2018 11:52
-
-
Save fpaint/1875df30dd849f13b4d320b94c608cc0 to your computer and use it in GitHub Desktop.
Component wrapper with async props loading
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
| class @Fetcher extends React.Component | |
| @propTypes: | |
| endpoint: PropTypes.string | |
| content: PropTypes.func | |
| onError: PropTypes.func | |
| constructor: (props)-> | |
| super(props) | |
| this.state = | |
| data: this.props.data || null | |
| fetch: -> | |
| return if this.state.data | |
| self = this | |
| promise = axios.get(this.props.endpoint) | |
| promise.then (res)-> self.setState(data: res.data) | |
| promise.catch (err)-> self.props.onError?(err.response) | |
| componentDidMount: -> this.fetch() | |
| render: -> | |
| if(this.state.data) | |
| props = _.extend {}, this.state.data, this.props.props | |
| React.createElement(this.props.content, props) | |
| else | |
| this.props.children || 'Загрузка...' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment