index.js 1.78 KB
Newer Older
zhanghaozhe committed
1 2 3
import React, { Component } from 'react';
import ReactDOM from 'react-dom'
import { HashLoader } from "react-spinners";
baiguangyao committed
4 5
import './loading.scss'

zhanghaozhe committed
6

zhanghaozhe committed
7
const container = document.body
zhanghaozhe committed
8 9 10 11

class Loading extends Component {

    static defaultProps = {
zhanghaozhe committed
12 13 14 15 16 17 18 19
        text: '加载中',
        fake: 0
    }

    state = {
        isLoading: true
    }

zhanghaozhe committed
20 21 22 23 24 25 26 27 28
    componentDidMount() {
        if(!this.props.isLoading){
            this.setState({
                isLoading: false
            })
        }
    }


zhanghaozhe committed
29 30 31 32 33 34 35 36 37 38
    componentDidUpdate(prevProps) {
        let {isLoading, fake} = this.props
        if (!isLoading) {
            if(fake){
                setTimeout(() => {
                    this.setState({
                        isLoading
                    })
                }, fake)
            }else {
wangshuo committed
39 40 41 42
                if(prevProps.isLoading) {
                    this.setState(()=>({
                        isLoading: false
                    }))
zhanghaozhe committed
43 44
                }
            }
wangshuo committed
45 46 47 48 49 50
        }else{
            if(prevProps.isLoading !== isLoading) {
                this.setState(()=>({
                    isLoading: true
                }))
            }
zhanghaozhe committed
51
        }
zhanghaozhe committed
52 53
    }

baiguangyao committed
54
    render() {
zhanghaozhe committed
55
        const innerLoading =
baiguangyao committed
56
            <div className="loading">
zhanghaozhe committed
57 58
                <div className="loading-wrapper">
                    <HashLoader
zhanghaozhe committed
59 60 61 62
                        css={{
                            display: 'block',
                            marginTop: '-100px'
                        }}
zhanghaozhe committed
63 64 65 66 67
                        size={50}
                        color={'#09f'}
                    />
                    <p>{this.props.text}</p>
                </div>
baiguangyao committed
68
            </div>
zhanghaozhe committed
69 70

        return (
zhanghaozhe committed
71
            this.state.isLoading ? ReactDOM.createPortal(innerLoading, container) : this.props.children
zhanghaozhe committed
72
        );
baiguangyao committed
73 74
    }
}
zhanghaozhe committed
75 76

export default Loading;