Sign Up

Join DevzConnect — where devs connect, code, and level up together. Got questions? Stuck on a bug? Or just wanna help others crush it? Jump in and be part of a community that gets it

Have an account? Sign In

Have an account? Sign In Now

Sign In

Welcome back to DevzConnect — where devs connect, code, and level up together. Ready to pick up where you left off? Dive back in, ask questions, share wins, or help others crush their goals!

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

Please type your username.

Please type your E-Mail.

Please choose an appropriate title for the question so it can be answered easily.

Please choose the appropriate section so the question can be searched easily.

Please choose suitable Keywords Ex: question, poll.

Browse
Type the description thoroughly and in details.

Choose from here the video type.

Put Video ID here: https://www.youtube.com/watch?v=sdUUx5FdySs Ex: "sdUUx5FdySs".

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

DevzConnect

DevzConnect Logo DevzConnect Logo

DevzConnect Navigation

  • Home
  • About
  • Blog
  • Contact
Search
Ask A Question

Mobile menu

Close
Ask A Question
  • Home
  • About
  • Blog
  • Contact
Home/ Questions/Q 503
Next
In Process

DevzConnect Latest Questions

nicko
  • 0
  • 0
nickoBeginner
Asked: February 20, 20252025-02-20T00:57:13+00:00 2025-02-20T00:57:13+00:00In: ReactJs

How do you implement drag and drop in React?

  • 0
  • 0

An explanation of drag-and-drop in React.

beginnerinterviewquestionsreactreactjs
1
  • 1 1 Answer
  • 284 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report
Leave an answer

Leave an answer
Cancel reply

Browse

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Finn Phillips
    Finn Phillips Beginner
    2025-02-22T05:23:39+00:00Added an answer on February 22, 2025 at 5:23 am

    ⚡ Implementing Drag and Drop in React

    You can implement drag and drop in React using either:

    1. Native HTML5 Drag & Drop API (for simple cases)
    2. Libraries like react-beautiful-dnd or react-dnd (for complex interactions)

    Let me walk you through both approaches. 🚀


    ✅ 1️⃣ Native HTML5 Drag & Drop API (Simple Example)

    Here’s how to create a simple draggable list where you can reorder items.


    🛠️ Example: Draggable List

    import { useState } from 'react';
    
    const DraggableList = () => {
    const [items, setItems] = useState(['Item 1', 'Item 2', 'Item 3']);
    const [draggedItemIndex, setDraggedItemIndex] = useState(null);
    
    const handleDragStart = (index) => setDraggedItemIndex(index);
    
    const handleDragOver = (e) => e.preventDefault();
    
    const handleDrop = (index) => {
    const updatedItems = [...items];
    const [draggedItem] = updatedItems.splice(draggedItemIndex, 1);
    updatedItems.splice(index, 0, draggedItem);
    setItems(updatedItems);
    setDraggedItemIndex(null);
    };
    
    return (
    <ul>
    {items.map((item, index) => (
    <li
    key={item}
    draggable
    onDragStart={() => handleDragStart(index)}
    onDragOver={handleDragOver}
    onDrop={() => handleDrop(index)}
    style={{
    padding: '10px',
    margin: '5px',
    backgroundColor: '#f0f0f0',
    border: '1px solid #ccc',
    cursor: 'grab',
    }}
    >
    {item}
    </li>
    ))}
    </ul>
    );
    };
    
    export default DraggableList;

    ⚡ Key Concepts:

    • draggable: Makes the item draggable.
    • onDragStart: Captures the index of the dragged item.
    • onDragOver: Prevents the default to allow dropping.
    • onDrop: Reorders items when dropped.

    ✅ 2️⃣ Using react-beautiful-dnd (Advanced Drag & Drop)

    For more complex UIs (like Trello-style boards), use react-beautiful-dnd.


    📦 Install the Library:

    npm install react-beautiful-dnd

    🛠️ Example: Reorderable List with react-beautiful-dnd

    import { DragDropContext, Droppable, Draggable } from 'react-beautiful-dnd';
    import { useState } from 'react';
    
    const DnDList = () => {
    const [items, setItems] = useState([
    { id: '1', content: 'Item 1' },
    { id: '2', content: 'Item 2' },
    { id: '3', content: 'Item 3' },
    ]);
    
    const handleOnDragEnd = (result) => {
    if (!result.destination) return;
    
    const updatedItems = Array.from(items);
    const [movedItem] = updatedItems.splice(result.source.index, 1);
    updatedItems.splice(result.destination.index, 0, movedItem);
    
    setItems(updatedItems);
    };
    
    return (
    <DragDropContext onDragEnd={handleOnDragEnd}>
    <Droppable droppableId="droppable-list">
    {(provided) => (
    <ul {...provided.droppableProps} ref={provided.innerRef}>
    {items.map((item, index) => (
    <Draggable key={item.id} draggableId={item.id} index={index}>
    {(provided, snapshot) => (
    <li
    ref={provided.innerRef}
    {...provided.draggableProps}
    {...provided.dragHandleProps}
    style={{
    userSelect: 'none',
    padding: '10px',
    margin: '5px',
    backgroundColor: snapshot.isDragging ? '#d3d3d3' : '#f0f0f0',
    border: '1px solid #ccc',
    ...provided.draggableProps.style,
    }}
    >
    {item.content}
    </li>
    )}
    </Draggable>
    ))}
    {provided.placeholder}
    </ul>
    )}
    </Droppable>
    </DragDropContext>
    );
    };
    
    export default DnDList;

    ⚡ Key Concepts in react-beautiful-dnd:

    • DragDropContext: The root wrapper.
    • Droppable: Defines a drop zone (like a list or board).
    • Draggable: Makes an item draggable.
    • onDragEnd: Handles what happens after a drag ends (e.g., reorder items).

    🔥 Which Approach Should You Use?

    • 🟢 Native API → Great for simple drag and drop (like moving a single item).
    • 🔥 react-beautiful-dnd → Best for complex UIs (e.g., Kanban boards, multi-lists).
      • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 226
  • Answers 144
  • Best Answers 4
  • Users 114
  • Popular
  • Answers
  • nicko

    Understanding Debounce in React: Best Practices for Optimizing API Calls and ...

    • 36 Answers
  • nicko

    How does React Server-Side Rendering (SSR) improve SEO and performance ...

    • 2 Answers
  • nicko

    What is the difference between props and state in react?

    • 2 Answers
  • blackpass biz
    blackpass biz added an answer Hey would you mind sharing which blog platform you're working… February 1, 2026 at 6:33 am
  • divisibility
    divisibility added an answer I am regular visitor, how are you everybody? This post… January 18, 2026 at 4:41 am
  • stashpatrick login
    stashpatrick login added an answer Normally I do not learn post on blogs, however I… January 17, 2026 at 11:15 pm

Related Questions

  • токарный станок чпу по металлу

    • 0 Answers
  • Understanding Debounce in React: Best Practices for Optimizing API Calls and ...

    • 36 Answers
  • How does React Server-Side Rendering (SSR) improve SEO and performance ...

    • 2 Answers
  • How do you create reusable components?

    • 1 Answer
  • How do you optimize React apps for performance?

    • 1 Answer

Top Members

Chloe Stewart

Chloe Stewart

  • 0 Questions
  • 51 Points
Teacher
Bryan Williamson

Bryan Williamson

  • 0 Questions
  • 37 Points
Beginner
Finn Phillips

Finn Phillips

  • 0 Questions
  • 35 Points
Beginner

Trending Tags

accsmarket.net beginner contextapi debounce interviewquestions javascript leetcode mongo mongodb nextjs r9hqxc react reactjs seo ssr theory

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges

Footer

© 2025 DevzConnect. All Rights Reserved

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.