Add a <Loading> component
One great thing about Frontity is that a lot of the content is pre-fetched into the state so page transitions are virtually instant. Sometimes, however, the content is not already in the state and Frontity must fetch it from the WordPress REST API.
Doing this is not instant. You can see this by selecting the About Us
menu option. Depending on your network speed you may notice a slight delay and the content area of our page remains empty.
Whenever there's a delay such as occurs while data is being fetched it's a good idea to indicate to the user that something is actually happening, so let's add a <Loading>
component.
Create a new file in the components
directory and call it loading.js
. Add the following code:
Here we're passing in the animation frames that we want, and assigning it to a variable spin
which we then go on to use in the animation
property of our <Spinner>
component.
Note that the <Loading>
component doesn't need to access the state so we don't need to pass it to the connect
higher order component when we export it.
Now we need to display it. Let's add it as the first item in the <Switch>
component that's within our <Root>
component. Remember that we also need to import the component.
Now as we transition between pages our loading spinner is displayed whenever data is being fetched in the background. Sweet! 👌
Last updated