All posts
nextjs5 min read

Creating a Splash Screen in Next.js

A splash screen is a visual element that is displayed when an application is loading. It is a great way to provide a professional and polished look to your application, and can also be used to display important information such as branding, loading status, and more.

Introduction

A splash screen is a visual element that is displayed when an application is loading. It is a great way to provide a professional and polished look to your application, and can also be used to display important information such as branding, loading status, and more. In this blog post, we will go over how to create a splash screen in Next.js, a popular framework for building React-based web applications.

Create a new Next.js project

To get started, you will need to create a new Next.js project. You can do this by running the following command in your terminal:

npx create-next-app my-app

This will create a new directory called "my-app" with the basic structure of a Next.js application.

Create the Splash Screen component

In this step, we will create the component that will be used to display the splash screen. In the "my-app" directory, create a new file called "SplashScreen.js" and add the following code:

import React from 'react';

const SplashScreen = () => {
  return (
    <div>
      <h1>My App</h1>
      <p>Loading...</p>
    </div>
  );
};

export default SplashScreen;

This component simply displays the text "My App" and "Loading..." on the screen. You can customize this component to display whatever content you want on your splash screen.

Use the Splash Screen component

Now that we have created our splash screen component, we need to use it in our application. Open the "pages/index.js" file and import the SplashScreen component at the top of the file:

import SplashScreen from '../components/SplashScreen';

Then, replace the existing content of the "Index" component with the SplashScreen component:

const Index = () => {
  return (
    <SplashScreen />
  );
};

Hide the Splash Screen after loading

Finally, we need to hide the splash screen after the application has finished loading. We can use the "useEffect" hook to accomplish this. In the "SplashScreen.js" file, import the "useEffect" hook at the top of the file:

import { useEffect } from 'react';

Then, add the following code to the "SplashScreen" component:

const SplashScreen = () => {
  const [isLoading, setIsLoading] = useState(true);

  useEffect(() => {
    setTimeout(() => {
      setIsLoading(false);
    }, 3000);
  }, []);

  if (!isLoading) {
    return null;
  }

  return (
    <div>
      <h1>My App</h1>
      <p>Loading...</p>
    </div>
  );
};

In this code, we are using the "useEffect" hook to set a timeout of 3 seconds (3000 milliseconds). After 3 seconds, the "isLoading" state is set to "false", which causes the SplashScreen component to be hidden from the DOM.

Adding a spinner using react-spinners

Now that we have the basic structure of our splash screen, let's make it more professional by adding a spinner to indicate that the application is still loading. To do this, we will use the "react-spinners" library, which provides a variety of pre-built spinners that we can use.

First, we need to install the library by running the following command in the terminal:

npm install react-spinners

Then, in the "SplashScreen.js" file, import the spinner component you want to use at the top of the file:

import { CircleSpinner } from 'react-spinners';

After that, add the spinner component in your JSX code:

return (
    <div className="splash-screen">
      <h1>My App</h1>
      <CircleSpinner size={30} color='#3f51b5' loading={isLoading} />
    </div>
  );

You can customize the size and color of the spinner as well as other props according to your needs.

Styling with CSS

Finally, let's add some styling to make our splash screen look more polished. Create a new file called "SplashScreen.css" in the "components" directory and add the following styles:

.splash-screen {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  height: 100vh;
  background-color: #fff;
}

In this CSS code, we are using Flexbox to center the content of the splash screen both horizontally and vertically, and setting the background color to white.

Then, you need to import your CSS file into your component file:

import './SplashScreen.css';

With these steps, you should now have a functional and professional-looking splash screen in your Next.js application. Feel free to customize the component and styles to fit your specific needs.

Additional Tips

  • You can use the setTimeout function to delay the display of the splash screen for a few seconds before your application is ready, giving your users a better user experience.
  • If you want to show the splash screen only on the initial load of your application, you can use the useEffect hook to check if the application has already been loaded, and only show the splash screen if it is the first load.
  • You can also make use of localstorage to store some information if the application has already been loaded to check if it's first load or not.
  • You can also use react-transition-group library to make the transition between the splash screen and the main application more smooth.

Conclusion

Creating a splash screen in Next.js is a great way to enhance the user experience of your application. By following the steps outlined in this blog post, you can easily create a professional-looking splash screen that provides valuable information to your users while the application is loading.