Open In App

How to add Draggable Components in Next.js ?

Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we are going to learn how we can add Draggable Components 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 conditionally.

Approach: To add our Draggable Components we are going to use the react-draggable package. The react-draggable package helps us to add draggable components anywhere in our app. So first, we will install the react-draggable package and then we will add a draggable component 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-draggable package using the below command:

npm i react-draggable

Project Structure: It will look like this.

Adding the Draggable Component: After installing the react-draggable package we can easily add the Draggable Component in our app. For this example, we are going to add a draggable component to our homepage.

Add the below content in the index.js file:

Javascript




import Draggable from 'react-draggable';
import React from 'react'
  
export default function DraggableComponents() {
  return (
    <div>
      <h3>GeeksforGeeks - Draggable Components</h3>
      <Draggable>
        <div>We can move this text</div>
      </Draggable>
    </div>
  )
}


Explanation: In the above example first, we are importing our Draggable component from the installed package. After that, we are creating a new function named DraggableComponents and then we are using the Draggable Component that we just imported from the installed package. 

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

npm run dev

Output:


Last Updated : 05 Jan, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads