React 懒加载组件

Posted 袜子破了

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了React 懒加载组件相关的知识,希望对你有一定的参考价值。

 

 

 //组件第一次初始化的时候加载.

 

import React, {PropTypes} from ‘react‘;

//import AppComposer from ‘./views/App/AppComposer‘;
//import LoginComposer from ‘./views/Login/LoginComposer‘;
//import HomeComposer from ‘./views/Home/HomeComposer‘;
//import WatchListComposer from ‘./views/WatchList/WatchListComposer‘;
//import SystemLogComposer from ‘./views/SystemLog/SystemLogComposer‘;
//import TeamManageComposer from ‘./views/TeamManage/TeamManageComposer‘;
//import {UserQueryPage} from ‘./views/UserQuery/UserQueryComposer‘;
//import ClosedCaseComposer from ‘./views/ClosedCase/ClosedCaseComposer‘;
//import AccountInfoComposer from ‘./views/AccountInfo/AccountInfoComposer‘;


function createLazyLoadComponent(doLoadComponent){
    return class newClassComponent extends React.Component{

        constructor(){
            super(...arguments);
            this.state = {
                RealComponent : null
            };
        }

        componentDidMount(){
            //组件第一次初始化的时候加载.
            doLoadComponent((pageComponent)=>{
                this.setState({
                    RealComponent:pageComponent
                });
            });
        }


        render(){
            var {RealComponent} = this.state;
            var props = this.props;
            if (RealComponent){
                return <RealComponent {...props} />
            }
            return <div>loading...</div>
        }
    }
}


export const LoginComposer = createLazyLoadComponent((onLoadCallback)=>{
     require([‘./views/Login/LoginComposer‘], onLoadCallback);
});

export const HomeComposer = createLazyLoadComponent((onLoadCallback)=>{
    require([‘./views/Home/HomeComposer‘], onLoadCallback);
});

export const WatchListComposer = createLazyLoadComponent((onLoadCallback)=>{
    require([‘./views/WatchList/WatchListComposer‘], onLoadCallback);
});

export const SystemLogComposer = createLazyLoadComponent((onLoadCallback)=>{
    require([‘./views/SystemLog/SystemLogComposer‘], onLoadCallback);
});

export const TeamManageComposer = createLazyLoadComponent((onLoadCallback)=>{
    require([‘./views/TeamManage/TeamManageComposer‘], onLoadCallback);
});

export const ClosedCaseComposer = createLazyLoadComponent((onLoadCallback)=>{
    require([‘./views/ClosedCase/ClosedCaseComposer‘], onLoadCallback);
});

export const AccountInfoComposer = createLazyLoadComponent((onLoadCallback)=>{
    require([‘./views/AccountInfo/AccountInfoComposer‘], onLoadCallback);
});

export const UserQueryPage = createLazyLoadComponent((onLoadCallback)=>{
    require([‘./views/UserQuery/UserQueryComposer‘], ({UserQueryPage})=>{
        onLoadCallback(UserQueryPage);
    });
});

 

以上是关于React 懒加载组件的主要内容,如果未能解决你的问题,请参考以下文章

React懒加载组件实现

React拓展 - setState - 路由组件懒加载 - Hooks - Fragment - Context - PureComponent - 插槽 - 错误边界 - 组件通信方式总结(代码片

react 懒加载

react 懒加载

react 懒加载

React 懒加载组件