Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
October 24, 2021 10:22 pm GMT

How to create a Preloader in NextJS

There's always a need to have all the content of a webpage ready before it is displayed to whoever is visiting your web app/website.

In situations where the contents of the webpage aren't ready, people would have to wait for it to be displayed, and this causes a very high decline rate of people who visit your website.

In this article, we'll have a look at how we can build a loading screen component that is displayed whenever the contents of the page is about to be mounted onto the DOM.

Before you read this article any further, you should be familiar with:

  • React, a declarative JavaScript library for building user interfaces
  • NextJS, a framework of React, used for building production-ready applications
  • Conditional rendering in React
  • Animations in CSS

Getting started

In this article, well be using NextJS to set up our app, you can use create-react-app if you are not familiar with NextJS.

Let us start by installing the dependencies that we need in this project. Wed start by creating a nextjs app. The command below gets the dependencies that we need in a Nextjs app.

npx create-next-app [name-of-your-app]

Well make use of the "styled-component" library to style the loading screen component. Lets get the dependency above by typing the command below into our terminal.

npm install --save styled-components

The components in our Nextjs app

In this section, we are going to see the different files that make up the architecture of this project, and their respective functions below.

The pages directory is where all the routing of the app takes place. This is an out-of-the-box feature of Nextjs. It saves you the stress of hard hard-coding your independent routes.

  • pages/api: the api directory enables you to have a backend for your nextjs app, inside the same codebase, instead of the common way of creating separate repositories for your REST or GraphQL APIs and deploying them on backend hosting platforms like Heroku, and so on.

  • pages/_app.js: is where all our components get attached to the DOM. If you take a look at the component structure, youll see that all the components are passed as pageProps to the Component props too.

function MyApp({ Component, pageProps }) {  return (    <React.Fragment>      <Component {...pageProps} />    </React.Fragment>  );}export default MyApp;

It is like the index.js file in Create-React-App. The only difference here is that you are not hooking your app to the DOM node called root.

 React.render(document.getElementById("root"), <App />)
  • index.js is the default route in the pages folder. When you run the command below, it starts up a development server and the contents of index.js are rendered on the web page.
npm run dev

Building the loading screen component

The previous sections walked you through the process of installing the dependencies that are needed for building the loading screen component and the functions of each file in a typical Nextjs app.

In this section, we'll go through the step-by-step process of building the component itself.

First, we'll be taking a look at the style of the loader. We are using the styled component library for this purpose.

The Screen styled-component serves as the parent container that wraps the loading animation. It uses a fade keyframe to ensure the transition of the screen is properly utilized.

// loadingScreen.jsimport styled from "styled-components";const Screen = styled.div`  position: relative;  height: 100vh;  width: 100%;  opacity: 0;  animation: fade 0.4s ease-in forwards;  background: black;  @keyframes fade {    0% {      opacity: 0.4;    }    50% {      opacity: 0.8;    }    100% {      opacity: 1;    }  }`;

The snippet below shows the Balls styled component. It serves as a container for the child elements in it. The corresponding divs in the container are the balls that we'll be animating.

You'll notice that there are unique classNames assigned to each div element in the container. This is for us to be able to set an animation-delay property on each ball so that the oscillating effect can be seen properly.

import styled from "styled-components";const Balls = styled.div`  display: flex;  position: absolute;  top: 50%;  left: 50%;  transform: translate(-50%, -50%);  .ball {    height: 20px;    width: 20px;    border-radius: 50%;    background: #1b5299;    margin: 0 6px 0 0;    animation: oscillate 0.7s ease-in forwards infinite;  }  .one {    animation-delay: 0.5s;  }  .two {    animation-delay: 1s;  }  .three {    animation-delay: 2s;  }  @keyframes oscillate {    0% {      transform: translateY(0);    }    50% {      transform: translateY(20px);    }    100% {      transform: translateY(0);    }  }`;

If you're new to animations in CSS. You can check this guide that explains the fundamentals.

Now that you have seen the styles of the components above. We'll go ahead to reference the styles in the LoadingScreeen component below.

import React from "react"import styled from "styled-components"const Screen = styled.div``const Balls = styled.div``const LoadingScreen = () => {  return (    <Screen>      <Balls>        <div className="ball one"></div>        <div className="ball two"></div>        <div className="ball three"></div>      </Balls>    </Screen>  );};export default LoadingScreen;

Implementing the preloader functionality

We've seen the function of the loading screen styles and how the animation works behind the scene.

In this section, we'll be importing the component into _app.js. Take a look at it below.

import LoadingScreen from "../src/components/LoadingScreen";function MyApp({ Component, pageProps }) {  const [loading, setLoading] = React.useState(false);  React.useEffect(() => {    setLoading(true);    setTimeout(() => setLoading(false), 5000);  }, []);  return (    <>      {!loading ? (        <React.Fragment>          <Component {...pageProps} />        </React.Fragment>      ) : (        <LoadingScreen />      )}    </>  );}export default MyApp

The snippet above shows how we've used conditional rendering to check the state of the loading component. We had to create a local state variable that holds the current state with the useState React hook.

const [loading, setLoading] = React.useState(false)

The initial state of the loader is set to a boolean value, false.

The useEffect hook is a lifecycle method in React that is fired whenever there's a change in the DOM. It combines all the lifecycle methods that a typical class-based component will have.

  React.useEffect(() => {    setLoading(true);    setTimeout(() => setLoading(false), 5000);  }, []);

By setting the initial "falsy" state of the loader to be true in the useEffect hook. We're telling the browser to display the loader with the help of the setTimeout function for a duration of five seconds.

At the end of the timeframe, the browser should've loaded all the contents that need to be on the webpage.

Conclusion

Below is a GIF that shows what we've been building all along.

preloader demo site

Although, this is an approach that helps us to have the content on a webpage/site available. If you are dealing with dynamic data, say from an API endpoint, the approach will be a little bit different.

You can decide to make use of this react-spinners package if you don't want to spend time creating a custom loading component all by yourself.

Thank you for reading this article. Kindly leave your thoughts in the comments section, and share this article with your peers, Thanks.


Original Link: https://dev.to/seven/how-to-create-a-preloader-in-nextjs-15n8

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To