zoukankan      html  css  js  c++  java
  • React 自写Loading

    import React, {Component} fro'react'
    import PropTypes from 'prop-types'
    import Animated from 'animated/lib/targets/react-dom'
    
    class Loader extends Component {
    
        constructor(props) {
            super(props);
            const animate = new Animated.Value(0);
            animate.addListener(this.changeOpacity.bind(this));
            this.state = {
                animate: animate,
                value: 0
            }
        }
    
        changeOpacity({value}) {
            if (value === 0) this.setState({value: 0});
        }
    
        componentDidUpdate(prevProps) {
            const {active} = this.props;
            if (!prevProps.active && active) {
                this.state.animate.setValue(0.58);
                this.setState({value: 0.58});
            } else if (prevProps.active && !active) {
                Animated.timing(this.state.animate, {toValue: 0}).start();
            }
        }
    
        render() {
            const {text} = this.props;
            if (this.state.value === 0) return false;
            return (
                <div>
                    <Animated.div
                        className="page-block"
                        style={{opacity: this.state.animate}}
                    />
                    <div className="loading-text">{text}</div>
                    <div className="loading-wrapper">
                        <div className="spinner">
                            <div className="double-bounce1"/>
                            <div className="double-bounce2"/>
                        </div>
                    </div>
                </div>
            )
        }
    }
    
    Loader.propTypes = {
        // 加载的文本
        text: PropTypes.string.isRequired,
        // 是否激活状态
        active: PropTypes.bool.isRequired
    };
    
    Loader.defaultProps = {
        text: 'Loading',
        active: false
    };
    
    export default Loader

    使用

    <Loader active={this.state.status}/>
  • 相关阅读:
    JDOJ 2197: 校门外的树
    简单线段树知识点详解
    求GCD(最大公约数)的两种方式
    USACO Buying Feed, II
    USACO Dueling GPS's
    USACO Milking Cows
    NOIP 2014 比例简化
    USACO Clumsy Cows
    JDOJ 1140: 完数
    NOIP 2008 火柴棒等式
  • 原文地址:https://www.cnblogs.com/it-Ren/p/14138532.html
Copyright © 2011-2022 走看看