React - Display loading screen while DOM is rendering?

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

    I'm using react-progress-2 npm package, which is zero-dependency and works great in ReactJS.

    https://github.com/milworm/react-progress-2

    Installation:

    npm install react-progress-2

    Include react-progress-2/main.css to your project.

    import "node_modules/react-progress-2/main.css";

    Include react-progress-2 and put it somewhere in the top-component, for example:

    import React from "react";
    import Progress from "react-progress-2";
    
    var Layout = React.createClass({
    render: function() {
        return (
            
    {/* other components go here*/}
    ); } });

    Now, whenever you need to show an indicator, just call Progress.show(), for example:

    loadFeed: function() {
        Progress.show();
        // do your ajax thing.
    },
    
    onLoadFeedCallback: function() {
        Progress.hide();
        // render feed.
    }
    

    Please note, that show and hide calls are stacked, so after n-consecutive show calls, you need to do n hide calls to hide an indicator or you can use Progress.hideAll().

提交回复
热议问题