React - Display loading screen while DOM is rendering?

前端 未结 19 1700
我在风中等你
我在风中等你 2020-11-28 00:29

This is an example from Google Adsense application page. The loading screen displayed before the main page showed after.

I don\'t know how to do the same th

19条回答
  •  栀梦
    栀梦 (楼主)
    2020-11-28 00:46

    You can easily do that by using lazy loading in react. For that you have to use lazy and suspense from react like that.

    import React, { lazy, Suspense } from 'react';
    
    const loadable = (importFunc, { fallback = null } = { fallback: null }) => {
      const LazyComponent = lazy(importFunc);
    
      return props => (
        
          
        
      );
    };
    
    export default loadable;
    

    After that export your components like this.

    export const TeacherTable = loadable(() =>
      import ('./MainTables/TeacherTable'), {
        fallback: ,
      });
    

    And then in your routes file use it like this.

     
    

    Thats it now you are good to go everytime your DOM is rendering your Loading compnent will be displayed as we have specified in the fallback property above. Just make sure that you do any ajax request only in componentDidMount()

提交回复
热议问题