How to use react-draggable module in React JS ?
Last Updated :
26 Jul, 2024
In React JS, the react-draggable module facilitates element dragging by utilizing CSS Transforms, enabling items to be draggable from any starting position. To implement this, the following approach can be employed in ReactJS.
Prerequisites:
Approach to use react-draggable:
We've showcased the utilization of the react-draggable module in our ReactJS application. By importing and incorporating the Draggable component into our main App component, any content within it becomes draggable across the screen using the mouse. As illustrated in the accompanying GIF, a div containing the text "This line can be moved now!" exemplifies this functionality.
Draggable Props:
- defaultClassName: It is used to denote the class name for the draggable UI.
- defaultPosition: It is used to denote the default x and y coordinate where the dragging should be started.
- disabled: It is used to disable the component.
- onMouseDown: It is a callback function that is triggered on the user mouses down event.
- onStart: It is a callback function that is triggered when dragging starts.
- onDrag: It is a callback function that is triggered while dragging.
- onStop: It is a callback function that is triggered when dragging stops.
- scale: It is used to denote the scale of the canvas where the dragging is performed.
Steps to Create React Application And Installing Module:
Step 1: Create a React application using the following command:
npx create-react-app foldername
Step 2: After creating your project folder i.e. foldername, move to it using the following command:
cd foldername
Step 3: Installing the required module using the following command:
npm install react-draggable
Project Structure:
Project StructureThe updated dependencies in package.json file will look like:
"dependencies": {
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-draggable": "^4.4.6",
"react-scripts": "5.0.1",
"web-vitals": "^2.1.4",
}
Example: Now write down the following code in the App.js file.
CSS
body {
background-color: antiquewhite;
}
.wrapper {
width: 100%;
display: flex;
flex-direction: column;
align-items: center;
font-size: x-large;
font-family:
'Trebuchet MS', 'Lucida Sans Unicode',
'Lucida Grande', 'Lucida Sans', Arial, sans-serif;
}
.dragdiv {
padding: 8rem;
color: blueviolet;
cursor: grabbing;
font-size: medium;
font-weight: 600;
font-family:
'Trebuchet MS', 'Lucida Sans Unicode',
'Lucida Grande', 'Lucida Sans', Arial, sans-serif;
}
JavaScript
import React from 'react';
import Draggable from 'react-draggable';
import "./App.css"
import { Flex } from '@chakra-ui/react';
export default function App() {
return (
<>
<div className='wrapper'>
<h4>React-Draggable Module</h4>
<div className='dragdiv'>
<Draggable>
<div>This line can be moved now!</div>
</Draggable>
</div>
</div>
</>
);
}
Step to Run Application: Run the application using the following command from the root directory of the project:
npm start
Output: Now open your browser and go to http://localhost:3000
Similar Reads
How to use react-dropzone module in ReactJS ? React-Dropzone module is a simple React hook that is used to create an HTML5-compliant drag-drop zone for n number of files. We can use this module to provide a way for users to drop and drop their multiple files, and then we can handle these files as per the business requirement.Prerequisites:NodeJ
2 min read
How to use react-window Module in React JS? React window works by only rendering part of a large data set. This module is very easy to integrate and reduces the amount of time which generally takes a while to render. This module avoids the over-allocation of DOM nodes. We can use the react-window module in React JS using the following approac
2 min read
How to use Bootstrap in React JS ? Explore the seamless integration of Bootstrap into React JS for enhanced styling and responsive web development. This comprehensive guide provides step-by-step instructions, covering the installation process, utilization of Bootstrap components, and best practices to harness the power of both techno
3 min read
How to Use react-select in React Application? React Select is a flexible and handy dropdown library for React that handles multi-select, loading data asynchronously, custom options, and more in your components with ease. In this article, we will see how to use react-select in React Application. PrerequisiteNode.js installednpm or yarn package m
2 min read
How to create Header in React JS ? The Header is an important element of a websiteâs design. It's the first impression of the website. It provides useful links to other areas of the website that the user may want to visit. In this article, we will create a functioning Header using React JS and Material UI.Prerequisites:NPM & Node
2 min read
How to use Paper Component in ReactJS ? In Material Design, the physical properties of paper are translated to the screen. Material UI for React has this component available for us and it is very easy to integrate. We can use the Paper Component in ReactJS using the following approach. Prerequisites:React JSMaterial UIApproachTo use Paper
2 min read