Skip to content

DnD how to differentiate elements when dragging them #3904

Answered by reidbarber
abhishekmg asked this question in Q&A
Discussion options

You must be logged in to vote

You can share state of what's being dragged to the drop target.

  let [currentlyDragging, setCurrentlyDragging] = React.useState(null);

  let onDragStart = (e) => {
    setCurrentlyDragging(e.keys);
  };

  let onDragEnd = (e) => {
    setCurrentlyDragging(null);
  };

  <Listbox
    onDragStart={onDragStart}
    onDragEnd={onDragEnd}
  ...

  // Then pass currentlyDragging to your drop target
  <DropTarget currentlyDragging={currentlyDragging} />

Example: https://codesandbox.io/s/dnd-custom-dropzone-cl9s01?file=/src/App.js

Replies: 1 comment 2 replies

Comment options

You must be logged in to vote
2 replies
@devongovett
Comment options

@abhishekmg
Comment options

Answer selected by abhishekmg
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
3 participants