dnd-kit icon indicating copy to clipboard operation
dnd-kit copied to clipboard

How do I get access to the Draggable data once it is over a Droppable?

Open alchermd opened this issue 2 years ago • 2 comments

Copying the example from the documentation quick start:

import React from 'react';
import {useDroppable} from '@dnd-kit/core';

function Droppable(props) {
  const {isOver, setNodeRef} = useDroppable({
    id: 'droppable',
  });
  const style = {
    color: isOver ? 'green' : undefined,
  };
  
  
  return (
    <div ref={setNodeRef} style={style}>
      {props.children}
    </div>
  );
}

How do I conditionally style the droppable depending on the draggable's data?

function Droppable(props) {
  // ...
  const supportedTypes = ["foo", "bar"];
  const style = {
    color: isOver ? 'green' : undefined,
  };
  
  const draggableCurrentlyOver = ???;

  if (supportedTypes.includes(draggableCurrentlyOver.data.type)) {
    style.color = "red" 
  }
  
  // ...
}

alchermd avatar Jun 03 '24 00:06 alchermd

Solved: Not found in the docs but active is also exposed by the useDroppable hook.

alchermd avatar Jun 03 '24 01:06 alchermd

Thank you for updating your issue with a solution. Saved me a headache. This should definitely be documented!

cryogen avatar Oct 05 '24 11:10 cryogen