Guest User

Untitled

a guest
Mar 21st, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.69 KB | None | 0 0
  1. import React, { Component } from 'react';
  2.  
  3. export default bindLoading = ({ loadingFunction }) => (Child) => {
  4. return class extends Component {
  5. constructor(props) {
  6. super(props);
  7.  
  8. this.state = { loading: false };
  9. }
  10.  
  11. componentDidMount() {
  12. const function = Child[loadingFunction];
  13. const isPromise = function instanceof Promise;
  14.  
  15. if (isPromise) return function();
  16.  
  17. this.setState({ loading: true });
  18. function()
  19. .then(() => this.setState({ loading: false }))
  20. .catch(() => this.setState({ loading: false }))
  21. }
  22.  
  23. render() {
  24. const { loading } = this.state;
  25. return <Child loading={loading} { ...this.props} />
  26. }
  27. }
  28. }
Add Comment
Please, Sign In to add comment