Add Comment Section in Next.js
Last Updated :
29 Jul, 2024
In this article, we are going to learn how we can add a Comment Section in NextJs. Using the comment section users can write their thoughts and queries in your NextJs app.
NextJS is a React-based framework. It has the power to Develop beautiful Web applications for different platforms like Windows, Linux, and mac. The linking of dynamic paths helps in rendering your NextJS components conditionally.
Approach: To add our Comment section we are going to use the Disqus platform. Disqus is a networked community platform used by hundreds of thousands of sites all over the web. So first, we will create an account in the Disqus platform and install the Disqus package and then we will create a new file in which we will write code for our comment section. Then on our homepage, we will import our comment section.
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 Disqus package using the below command:
npm install disqus-react
Project Structure: It will look like this.

Create Account in Disqus: Next we have to create a new account in Disqus. So to do this follow the below steps:
1. Go to Disqus's Official Site and Click on Get Started. Then click on Sign Up.

2. After Signing up, Click on the 'I want to install Disqus on my site' Button.

3. After that, Enter your website name and choose a category.

That's it. Now we are ready to add the Comment section in our NextJs app.
Adding Comment Section: Now to add our comment section we are going to create a new folder in our gfg directory with the name 'components' and inside this folder, we will create a new javascript file with the name 'Comment.js'.
Add the below content in our Comment.js file:
JavaScript
import {DiscussionEmbed} from "disqus-react"
const Comments = () => {
const disqusShortname = "Demo-GfG"
const disqusConfig = {
url: "http://localhost:3000",
identifier:'123',
title: "Demo Post"
}
return (
<div>
<DiscussionEmbed
shortname={disqusShortname}
config={disqusConfig}
/>
</div>
)
}
export default Comments;
In the above code, we are first importing DiscussionEmbed from our disqus-react. After that, we are creating a new component with the name Comments and inside this component, we are storing the disqusShortname. Website URL, identifier, and title in different constant variables. Then we are returning our DiscussionEmbed function.
Now we can import the above file to our homepage to display our comment section.
Add the below content in the index.js file:
JavaScript
import React from 'react'
import Comments from '../components/Comment'
export default function Text() {
return (
<div>
<h1>Comments - GeeksforGeeks</h1>
<Comments/>
</div>
)
}
Now when we run our NextJs app the index.js file will act as the homepage of the app.
Steps to run the application: Run the below command in the terminal to run the app.
npm run dev
Output:
Similar Reads
How to add ESLint in Next.js ? ESLint is a popular linting tool for identifying and fixing issues in JavaScript code. Adding ESLint to your Next.js project ensures that your code follows best practices and maintains a consistent style. This enhances code quality, helps catch errors early, and makes the codebase easier to maintain
3 min read
How to add CodeBlock in Next.js ? In this article, we are going to learn how we can add CodeBlock 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. The linking of dynamic paths helps in rendering your NextJS components conditiona
2 min read
How to Add Stylesheet in Next.js ? In Next.js, adding a stylesheet enhances your app's styling capabilities. Import CSS files directly in your components or pages using ES6 import syntax. Next.js optimizes and includes these styles in the build process, ensuring efficient and modular CSS management.In this post, we are going to learn
4 min read
Next.js Script Component The Next.js Script component helps you manage when and how scripts are loaded to make your site perform better. It offers three strategies: beforeInteractive for scripts that need to load immediately, afterInteractive for scripts that are needed after the page becomes interactive but arenât crucial
5 min read
How to add layout in Next.js ? Next.js is a React-based full-stack framework developed by vercel that enables functionalities like pre-rendering of web pages. Unlike traditional react app where the entire app is loaded on the client, Next.js allow the web page to be rendered on the server, which is great for performance and SEO.
3 min read