useDroppable

Arguments

1
interface UseDroppableArguments {
2
id: string;
3
disabled?: boolean;
4
data?: Record<string, any>;
5
}
Copied!

Identifier

The id argument is a string that should be a unique identifier, meaning there should be no other droppable elements that share that same identifier within a given DndContext provider.
If you're building a component that uses both the useDroppable and useDraggable hooks, they can both share the same identifier since droppable elements are stored in a different key-value store than draggable elements.

Disabled

Since hooks cannot be conditionally invoked, use the disabled argument and set it to true if you need to temporarily disable a droppable area.

Data

The data argument is for advanced use-cases where you may need access to additional data about the droppable element in event handlers, modifiers or custom sensors.
For example, if you were building a sortable preset, you could use the data attribute to store the index of the droppable element within a sortable list to access it within a custom sensor.
1
const {setNodeRef} = useDroppable({
2
id: props.id,
3
data: {
4
index: props.index,
5
},
6
});
Copied!
Another more advanced example where the data argument can be useful is create relationships between draggable nodes and droppable areas, for example, to specify which types of draggable nodes your droppable accepts:
1
import {DndContext, useDraggable, useDroppable} from '@dnd-kit/core';
2
3
function Droppable() {
4
const {setNodeRef} = useDroppable({
5
id: 'droppable',
6
data: {
7
accepts: ['type1', 'type2'],
8
},
9
});
10
11
/* ... */
12
}
13
14
function Draggable() {
15
const {attributes, listeners, setNodeRef, transform} = useDraggable({
16
id: 'draggable',
17
data: {
18
type: 'type1',
19
},
20
});
21
22
/* ... */
23
}
24
25
function App() {
26
return (
27
<DndContext onDragEnd={handleDragEnd}>
28
/* ... */
29
</DndContext>
30
);
31
32
function handleDragEnd(event) {
33
const {active, over} = event;
34
35
if (over && over.data.current.accepts.includes(active.data.current.type)) {
36
// do stuff
37
}
38
}
39
}
Copied!

Properties

1
{
2
rect: React.MutableRefObject<LayoutRect | null>;
3
isOver: boolean;
4
node: React.RefObject<HTMLElement>;
5
over: {id: UniqueIdentifier} | null;
6
setNodeRef(element: HTMLElement | null): void;
7
}
Copied!

Node

setNodeRef

In order for the useDroppable hook to function properly, it needs the setNodeRef property to be attached to the HTML element you intend on turning into a droppable area:
1
function Droppable(props) {
2
const {setNodeRef} = useDroppable({
3
id: props.id,
4
});
5
6
return (
7
<div ref={setNodeRef}>
8
{/* ... */}
9
</div>
10
);
11
}
Copied!

node

A ref to the current node that is passed to setNodeRef

rect

For advanced use cases, if you need the bounding rect measurement of the droppable area.

Over

isOver

Use the isOver boolean returned by the useDroppable hook to change the appearance or content displayed when a draggable element is dragged over your droppable container.

over

If you'd like to change the appearance of the droppable in response to a draggable being dragged over a different droppable container, check whether the over value is defined. Depending on your use-case, you can also read the id of the other droppable that the draggable item to make changes to the render output of your droppable component.

Last modified 5mo ago