Implement Drag and Drop using React Component
Last Updated :
04 Apr, 2024
To add drag and drop functionality in your React application, you can use libraries like "react-beautiful-dnd" or "react-dnd". These libraries provide components and hooks that simplify the process of creating draggable and droppable elements in your UI. By wrapping your draggable items with the appropriate components and defining drag-and-drop behaviour using event handlers, you can enable users to interact with elements by dragging and dropping them across different parts of your application. In this tutorial, we will discuss how to implement drag-and-drop functionality using React components.
Output Preview: Let us have a look at how the final output will look like
Prerequisites:
Steps to Create Application
Step 1: First, let's create a new React application using Create React App. Open your terminal and run the following command:
npx create-react-app drag-and-drop-demo
cd drag-and-drop-demo
Step 2: Next, install the React DnD library:
npm install react-dnd react-dnd-html5-backend
Project Structure:

Step 3: The updated dependencies in package.json file will look like:
"dependencies": {
"react": "^17.0.2",
"react-dnd": "^14.0.2",
"react-dnd-html5-backend": "^14.0.2",
}
Approach to implement Drag & Drop using React Component:
- Wrap your application with a DndProvider from react-dnd to enable drag and drop functionality.
- Define draggable items using the useDrag hook provided by react-dnd. This hook handles the drag behavior and provides the necessary properties like isDragging.
- Create drop zones where items can be dropped. Use the useDrop hook to define drop behavior and handle the onDrop event to capture dropped items.
- Use React's useState hook to manage the state of dropped items. Update the state when an item is dropped or removed to reflect changes in the UI.
- Apply CSS styles to visually indicate drag and drop interactions, such as changing opacity or border colors when dragging or hovering over drop zones.
Example: Implementing the Drag and Drop functionality. We'll create two components: DragItem for draggable items and DropZone for drop zones.
JavaScript
//App.js
import React, { useState } from 'react';
import { DndProvider } from 'react-dnd';
import { HTML5Backend } from 'react-dnd-html5-backend';
import DragItem from './DragItem';
import DropZone from './DropZone';
const App = () => {
const [droppedItems, setDroppedItems] = useState([]);
const handleDrop = (item) => {
setDroppedItems((prevItems) => [...prevItems, item]);
};
const handleRemoveItem = (index) => {
const updatedItems = [...droppedItems];
updatedItems.splice(index, 1);
setDroppedItems(updatedItems);
};
return (
<DndProvider backend={HTML5Backend}>
<div style={{
display: 'flex',
justifyContent: 'center',
alignItems: 'center',
height: '100vh'
}}>
<div style={{
border: '1px solid #ccc',
padding: '20px',
borderRadius: '5px'
}}>
<h1>Drag and Drop Example</h1>
<div style={{
display: 'flex',
justifyContent: 'space-around'
}}>
<div style={{
border: '1px solid #ccc',
padding: '10px', borderRadius: '5px'
}}>
<h2>Drag Items</h2>
<DragItem name="Item 1" />
<DragItem name="Item 2" />
<DragItem name="Item 3" />
</div>
<div style={{
border: '1px solid #ccc',
padding: '10px', borderRadius: '5px'
}}>
<h2>Drop Zone</h2>
<DropZone onDrop={handleDrop} />
{droppedItems.map((item, index) => (
<div
key={index}
style={{
border: '1px solid #ccc',
padding: '10px',
borderRadius: '5px',
marginTop: '10px',
backgroundColor: 'lightblue',
display: 'flex',
justifyContent: 'space-between',
alignItems: 'center',
}}>
<p>{item.name}</p>
<button onClick={
() => handleRemoveItem(index)}>
Remove
</button>
</div>
))}
</div>
</div>
</div>
</div>
</DndProvider>
);
};
export default App;
JavaScript
//DragItem.js
import React from 'react';
import { useDrag } from 'react-dnd';
const DragItem = ({ name }) => {
const [{ isDragging }, drag] = useDrag(() => ({
type: 'item',
item: { name },
collect: (monitor) => ({
isDragging: monitor.isDragging(),
}),
}));
return (
<div
ref={drag}
style={{
opacity: isDragging ? 0.5 : 1,
cursor: 'move',
border: '1px solid #ccc',
padding: '10px',
borderRadius: '5px',
margin: '5px',
backgroundColor: 'lightblue',
}}>
{name}
</div>
);
};
export default DragItem;
JavaScript
// DropZone.js
import React from 'react';
import { useDrop } from 'react-dnd';
const DropZone = ({ onDrop }) => {
const [{ isOver }, drop] = useDrop(() => ({
accept: 'item',
drop: (item) => onDrop(item),
collect: (monitor) => ({
isOver: monitor.isOver(),
}),
}));
return (
<div
ref={drop}
style={{
border: `1px dashed ${isOver ? 'green' : 'black'}`,
padding: '10px',
}}>
Drop here
</div>
);
};
export default DropZone;
Step to Run Application: Run the application using the following command from the root directory of the project
npm start
Output: Your project will be shown in the URL http://localhost:3000/

Note: After implementing the Drag and Drop functionality, you should be able to drag items from the DragItem component and drop them into the DropZone component. The dropped items should be rendered inside the DropZone.
Similar Reads
Drag and Drop File Upload Component with React Hooks
In this article, we're going to delve into creating a drag-and-drop feature for uploading files using React Hooks. Dragging and dropping makes it simple for users to add files to a web app. With React Hooks we can control state and execute actions in components simplifying the management of drag and
4 min read
ReactJS UI Ant Design Dropdown Component
Ant Design Library has this component pre-built, and it is very easy to integrate as well. Dropdown Component is used to provide a dropdown list, and it is used when the user has more than a few options to choose from. We can use the following approach in ReactJS to use the Ant Design Dropdown Compo
3 min read
How to Map Data into Components using ReactJS?
Mapping data in react js component is a comman practice to render the lists and repeating elements. In this article, we will have a users data and we will map it to react components to display the users information in the react app Prerequisites:React JSNodejs and npmJavaScript Array mapApproachTo m
3 min read
ReactJS UI Ant Design Message Component
Ant Design Library has this component pre-built, and it is very easy to integrate as well. In response to user operations, the Message Component is used for displaying global messages as feedback. We can use the following approach in ReactJS to use the Ant Design Message Component. Message Props: co
3 min read
Drag and Drop Sortable List Using ReactJS
Building a drag and drop sortable list in ReactJS allows users to rearrange items interactively by dragging and dropping. This feature enhances the user experience by providing dynamic way to manage lists or items. In this article, weâll explore how to implement a drag and drop sortable list in Reac
4 min read
Drag and Drop List Using TypeScript
A drag-and-drop sortable list in TypeScript lets users reorder items by dragging them to new positions. This is done using dragstart, dragend and dragover events for smooth DOM manipulation. What Weâre Going to CreateWeâll build a drag-and-drop sortable list with features like handling dragstart, dr
5 min read
ReactJS UI Ant Design Layout Component
Ant Design Library has this component pre-built, and it is very easy to integrate as well. The Layout component is used for handling the overall layout of a page. We can use the following approach in ReactJS to use the Ant Design Layout Component. Layout Props: className: It is used to pass the cont
3 min read
ReactJS Evergreen Alert Component
React Evergreen is a popular front-end library with a set of React components for building beautiful products as this library is flexible, sensible defaults, and User friendly. Alert Component is used for are urgent interruptions, requiring acknowledgment, that informs the user about a situation. We
2 min read
React Importing and Exporting Components
In React, components are the building blocks of any application. They allow developers to break down complex user interfaces into smaller, reusable pieces. To manage these components efficiently, you need to understand how to import and export them. In this article, we will explore the different met
6 min read
ReactJS UI Ant Design ConfigProvider Component
Ant Design Library has this component pre-built, and it is very easy to integrate as well. ConfigProvider Component is used to provide uniform configuration support for components. We can use the following approach in ReactJS to use the Ant Design ConfigProvider Component. ConfigProvider Props: auto
2 min read