zoukankan      html  css  js  c++  java
  • [Recompose] Show a Spinner While a Component is Loading using Recompose

    Learn how to use the 'branch' and 'renderComponent' higher-order components to show a spinner while a component loads.

    import React from 'react';
    import { lifecycle, branch, compose, renderComponent } from 'recompose';
    
    // Mock Configuration
    function getUser() {
        return new Promise((resolve, reject) => {
            setTimeout(() => resolve({
                name: 'Zhentian',
                status: 'active'
                                     }), 1500);
        })
    }
    
    const Spinner = () =>
        <div className="Spinner">
            <div className="loader">Loading...</div>
        </div>;
    
    const withUserData = lifecycle({
        getInitialState(){
            return {
                loading: true
            }
        },
        componentDidMount() {
            getUser()
                .then((user) => {
                    this.setState({...user, loading: false})
                })
        }
                               });
    
    
    const UserStyle = {
        position: 'relative',
        background: 'lightblue',
        display: 'inline-block',
        padding: '10px',
        cursor: 'pointer',
        marginTop: '50px'
    };
    
    
    
    const withSpinnerWhileLoading = branch(
        (props) => props.loading,
        renderComponent(Spinner)
    );
    
    const enhance = compose(
        withUserData,
        withSpinnerWhileLoading
    )
    
    let User5 = enhance(({ name, status }) => (
        <div style={UserStyle}>
            {name} - {status}
        </div>
    ));
    
    
    export default User5;
  • 相关阅读:
    Python之while循环
    Python之分支语句
    Python之变量
    Python开挂的吧!
    xshell 连接 ubuntu 16.04报错
    js中的script标签
    javascript中的事件学习总结
    【JAVAWEB学习笔记】04_JavaScript
    【JAVAWEB学习笔记】03_JavaScript
    【JAVAWEB学习笔记】02_HTML&CSS
  • 原文地址:https://www.cnblogs.com/Answer1215/p/6859338.html
Copyright © 2011-2022 走看看