React - Display loading screen while DOM is rendering?

前端 未结 19 1666
我在风中等你
我在风中等你 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 01:06

    I had to deal with that problem recently and came up with a solution, which works just fine for me. However, I've tried @Ori Drori solution above and unfortunately it didn't work just right (had some delays + I don't like the usage of setTimeout function there).

    This is what I came up with:

    index.html file

    Inside head tag - styles for the indicator:

    
    

    Now the body tag:

    And then comes a very simple logic, inside app.js file (in the render function):

    const spinner = document.getElementById('spinner');
    
    if (spinner && !spinner.hasAttribute('hidden')) {
      spinner.setAttribute('hidden', 'true');
    }
    

    How does it work?

    When the first component (in my app it's app.js aswell in most cases) mounts correctly, the spinner is being hidden with applying hidden attribute to it.

    What's more important to add - !spinner.hasAttribute('hidden') condition prevents to add hidden attribute to the spinner with every component mount, so actually it will be added only one time, when whole app loads.

提交回复
热议问题