index.js 1.61 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 39 40 41 42 43 44 45
    componentDidUpdate(prevProps) {
        let {isLoading, fake} = this.props
        if (!isLoading) {
            if(fake){
                setTimeout(() => {
                    this.setState({
                        isLoading
                    })
                }, fake)
            }else {
                if(prevProps.isLoading != isLoading){
                    this.setState({
                        isLoading
                    })
                }
            }
        }
zhanghaozhe committed
46 47
    }

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

        return (
zhanghaozhe committed
65
            this.state.isLoading ? ReactDOM.createPortal(innerLoading, container) : this.props.children
zhanghaozhe committed
66
        );
baiguangyao committed
67 68
    }
}
zhanghaozhe committed
69 70

export default Loading;