Skip to content

Instantly share code, notes, and snippets.

@fpaint
Created March 26, 2018 11:52
Show Gist options
  • Select an option

  • Save fpaint/1875df30dd849f13b4d320b94c608cc0 to your computer and use it in GitHub Desktop.

Select an option

Save fpaint/1875df30dd849f13b4d320b94c608cc0 to your computer and use it in GitHub Desktop.
Component wrapper with async props loading
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