Advertisement
Guest User

Untitled

a guest
Oct 14th, 2019
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.07 KB | None | 0 0
  1. import React from 'react'
  2. import { connect } from 'react-redux'
  3. import { initLoading, stopLoading } from '@/state/actions/loadingActions.js'
  4. import { cacheData } from '@/state/actions/dataActions.js'
  5.  
  6. export default connect((state, props) => {
  7. return {
  8. data: state.data[props.cache]
  9. }
  10. })(
  11. class DataLoader extends React.Component {
  12. constructor (props) {
  13. super(props)
  14.  
  15. this.state = {
  16. loading: true
  17. }
  18. }
  19.  
  20. async componentDidMount () {
  21. const { cache, load, dispatch } = this.props
  22.  
  23. this.setState({ loading: true })
  24.  
  25. dispatch(initLoading())
  26.  
  27. const data = await load()
  28.  
  29. dispatch(cacheData({
  30. [cache]: data
  31. }))
  32.  
  33. this.setState({ loading: false })
  34.  
  35. dispatch(stopLoading())
  36. }
  37.  
  38. render () {
  39. const { loading } = this.state
  40. const {
  41. children,
  42. cache,
  43. render,
  44. fallback = () => null,
  45. data
  46. } = this.props
  47.  
  48. const component = typeof children === 'function' ? children : render
  49.  
  50. return loading ? fallback() : component({ data })
  51. }
  52. }
  53. )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement