question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

active.data.current gets lost "on drag end".

See original GitHub issue

I have a draggable (also using an DragOverlay) defined as such:

  const { attributes, listeners, setNodeRef } = useDraggable({
    id: `${type}_${id}`,
    data: {
      id,
      type,
    },
  });

and I handle the onDragStart and onDragEnd where my DnDContext is rendering. If I console log active.data.current inside onDragStart, I can see the data I provided. Unfortunately, once I let go of the mouse and the drop should happen, active.data.current is just an empty object.

Using the latest version, I have no idea how to even replicate this bug or why this would ever happen. Any ideas?


Digging a little deeper, I see: https://github.com/clauderic/dnd-kit/blob/919f21af9382b88141b6305769836960d5844fa5/packages/core/src/components/DndContext/DndContext.tsx#L165 - “// It’s possible for the active node to unmount while dragging”. This is the only thing I see that would mess up with the data object and, it’s true, the container unmounts if I drag outside of it.

Is there any way to fix this gracefully or do I have to “suspend” my state somehow such that the item remains mounted until I finish my drag?

Issue Analytics

  • State:open
  • Created a year ago
  • Reactions:8
  • Comments:5

github_iconTop GitHub Comments

4reactions
Fabianopbcommented, Jul 29, 2022

I’m experiencing the same issue within a virtualized list when dragging the item far enough (meaning that the virtualization unmounts the original instance).

It seems a bug to me as the data information should be preserved on the active object, just like the id is preserved.

1reaction
growthwpcommented, Jul 7, 2022

@wiltsu @shawnshuang Assuming it is not a bug, as I noted, the only way for active.data.current to be missing is if the original draggable gets unmounted.

If you have something complex, you can just run a console.log on unmount to see if your draggable’s really unmounting:

useEffect(() => {
  return () => {
    console.log('Unmounted.');
  }
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

7.7 Drag and drop — HTML5 - W3C
To make an element draggable is simple: give the element a draggable attribute, and set an event listener for dragstart that stores the...
Read more >
How do I detect a HTML5 drag event entering and leaving the ...
Set a flag ( showDrag ) to true on dragenter and dragover of the html (or body) element. On dragleave set the flag...
Read more >
6.10 Drag and drop - HTML Standard - WhatWG
When the DataTransfer object is created, the dropEffect attribute is set to a string value. On getting, it must return its current value....
Read more >
Drag'n'Drop with mouse events - The Modern JavaScript Tutorial
These are the basics. Later we'll see how to add other features, such as highlighting current underlying elements while we drag over them....
Read more >
Advanced options - Microsoft Support
In the Direction box, indicate which adjacent cell becomes active. ... drag-and-drop Select this check box to move and to copy cells and...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found