PromiseRender.js 1.21 KB
Newer Older
ddcat1115's avatar
ddcat1115 committed
1 2 3 4
import React from 'react';
import { Spin } from 'antd';

export default class PromiseRender extends React.PureComponent {
afc163's avatar
afc163 committed
5
  state = {
6
    component: null,
afc163's avatar
afc163 committed
7
  };
8
  componentDidMount() {
9 10
    const ok = this.checkIsInstantiation(this.props.ok);
    const error = this.checkIsInstantiation(this.props.error);
afc163's avatar
afc163 committed
11 12 13
    this.props.promise
      .then(() => {
        this.setState({
14
          component: ok,
ddcat1115's avatar
ddcat1115 committed
15
        });
afc163's avatar
afc163 committed
16 17 18
      })
      .catch(() => {
        this.setState({
19
          component: error,
afc163's avatar
afc163 committed
20 21 22
        });
      });
  }
23 24 25 26 27 28 29 30 31 32
  // Determine whether the incoming component has been instantiated
  // AuthorizedRoute is already instantiated
  // Authorized  render is already instantiated, children is no instantiated
  // Secured is not instantiated
  checkIsInstantiation = (target) => {
    if (!React.isValidElement(target)) {
      return target;
    }
    return () => target;
  };
afc163's avatar
afc163 committed
33
  render() {
34 35 36
    const Component = this.state.component;
    return Component ? (
      <Component {...this.props} />
afc163's avatar
afc163 committed
37 38 39 40 41 42 43 44 45 46 47 48 49 50
    ) : (
      <div
        style={{
          width: '100%',
          height: '100%',
          margin: 'auto',
          paddingTop: 50,
          textAlign: 'center',
        }}
      >
        <Spin size="large" />
      </div>
    );
  }
ddcat1115's avatar
ddcat1115 committed
51
}