Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

How to add Skeleton Loading in NextJS ?

  • Last Updated : 15 Dec, 2021

In this article, we are going to learn how we can add Skeleton Loading in NextJS. NextJS is a React-based framework. It has the power to Develop beautiful Web applications for different platforms like Windows, Linux, and mac.

Approach: To add our skeleton loading we are going to use the react-loading-skeleton package. The react-loading-skeleton package helps us to add a skeleton loading anywhere in our app. So first, we will install the react-loading-skeleton package and then we will add a loading screen on our homepage.

Create NextJS Application: You can create a new NextJs project using the below command:

npx create-next-app gfg

 

Install the required package: Now we will install the react-loading-skeleton package using the below command:

npm i react-loading-skeleton

Project Structure: It will look like this.

Adding the Skeleton Loading: After installing the package we can easily add loading to our app. For this example, we are going to add a skeleton loading to our homepage.

index.js




import React, { useState } from 'react';
import Skeleton from 'react-loading-skeleton'
import 'react-loading-skeleton/dist/skeleton.css'
  
export default function SkeletonLoading(){
  const [checked, setChecked] = React.useState(false);
  
  const handleChange = () => {
    setChecked(!checked);
  };
  return (
    <div>
      <label>
        <input
          type="checkbox"
          checked={checked}
          onChange={handleChange}
        />
        Loading
      </label>
      <p>{checked?<Skeleton />: 
           <h2>
           NextJs Skeleton Loading - GeeksforGeeks
           </h2>}</p>
  
    </div>
  )
}

Explanation: In the above example first, we are importing our Skeleton component from the installed package. After that, we are using the Skeleton component inside a new function. We are also using react hook to check the current value in the checkbox. If the checkbox is checked then we are showing our loading screen.

Steps to run the application: Run the below command in the terminal to run the app.

npm run dev

Output:

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!