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 thing with React because if I make loading screen rendered by React component, it doesn't display while page is loading because it has to wait for DOM rendered before.
Updated:
I made an example of my approach by putting screen loader in index.html
and remove it in React componentDidMount()
lifecycle method.
Example: https://nguyenbathanh.github.io
Source: https://github.com/nguyenbathanh/react-loading-screen
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:Now, whenever you need to show an indicator, just call
Progress.show()
, for example:Please note, that
show
andhide
calls are stacked, so after n-consecutive show calls, you need to do n hide calls to hide an indicator or you can useProgress.hideAll()
.This could be done by placing the loading icon in your html file (index.html for ex), so that users see the icon immediately after the html file has been loaded.
When your app finishes loading, you could simply remove that loading icon in a lifecycle hook, I usually do that in
componentDidMount
.When your React app is massive, it really takes time for it to get up and running after the page has been loaded. Say, you mount your React part of the app to
#app
. Usually, this element in your index.html is simply an empty div:What you can do instead is put some styling and a bunch of images there to make it look better between page load and initial React app rendering to DOM:
After the page loads, user will immediately see the original content of index.html. Shortly after, when React is ready to mount the whole hierarchy of rendered components to this DOM node, user will see the actual app.
Note
class
, notclassName
. It's because you need to put this into your html file.If you use SSR, things are less complicated because the user will actually see the real app right after the page loads.