Open In App

How to Add Star Rating in Next.js ?

Last Updated : 25 Jul, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

We can add star rating in next.js application using NPM package or create a Star rating component with css styling and state management for interactivity, enhancing user feedback and engagement. In this article, we are going to learn how we can add Star Rating in NextJS.

Approach

To add our ratings we are going to use the react-stars package. The react-stars package helps us to integrate different types of ratings. So first, we will install the react-stars package and then we will add star ratings 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-stars package using the below command:

npm i react-stars

Project Structure: It will look like this.

The updated dependencies in the package.json file:

"dependencies": {
"next": "14.2.4",
"react": "^18",
"react-dom": "^18",
"react-stars": "^2.2.5"
}

Example: After installing the package we can easily add ratings in our app. For this example, we are going to add star ratings to our homepage.

JavaScript
// index.js

import React from 'react'
import ReactStars from 'react-stars'

export default function SkeletonLoading() {
    return (
        <div>
            <h2>NextJs Star Ratings - GeeksforGeeks</h2>
            <ReactStars count={5} size={24} color2={'#ffd700'} />
        </div>
    )
}

Explanation: In the above example first, we are importing our ReactStars component from the installed package. After that, we are using the component and setting the count, size, and color. Then we are displaying the Star Ratings.

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

npm run dev

Output:


Next Article

Similar Reads