Quick start
Eager to get started? This quick start guide will help you familiarize yourself with the core concepts of dnd kit.
Before getting started, make sure you have followed the installation steps outlined in the Installation guide.

Context provider

First, we'll set up the general structure of the app. In order for the useDraggable and useDroppable hooks to function correctly, you'll need to ensure that the components where they are used are wrapped within a <DndContext /> component:
App.jsx
1
import React from 'react';
2
import {DndContext} from '@dnd-kit/core';
3
4
import {Draggable} from './Draggable';
5
import {Droppable} from './Droppable';
6
7
function App() {
8
return (
9
<DndContext>
10
<Draggable />
11
<Droppable />
12
</DndContext>
13
)
14
}
Copied!

Droppable

Next, let's set up your first Droppable component. To do so, we'll be using the useDroppable hook. The useDroppable hook isn't opinionated about how your app should be structured. At minimum though, it requires you pass a ref to the DOM element that you would like to become droppable. You'll also need to provide a unique id attribute to all your droppable components.
When a draggable element is moved over your droppable element, the isOver property will become true.
Droppable.jsx
1
import React from 'react';
2
import {useDroppable} from '@dnd-kit/core';
3
4
function Droppable(props) {
5
const {isOver, setNodeRef} = useDroppable({
6
id: 'droppable',
7
});
8
const style = {
9
color: isOver ? 'green' : undefined,
10
};
11
12
13
return (
14
<div ref={setNodeRef} style={style}>
15
{props.children}
16
</div>
17
);
18
}
Copied!

Draggable

Next, let's take a look at implementing our first Draggable component. To do so, we'll be using the useDraggable hook.
The useDraggable hook isn't opinionated about how your app should be structured. It does however require you to be able to attach listeners and a ref to the DOM element that you would like to become draggable. You'll also need to provide a unique id attribute to all your draggable components.
After a draggable item is picked up, the transform property will be populated with the translate coordinates you'll need to move the item on the screen.
The transform object adheres to the following shape: {x: number, y: number, scaleX: number, scaleY: number}
Draggable.jsx
1
import React from 'react';
2
import {useDraggable} from '@dnd-kit/core';
3
4
function Draggable(props) {
5
const {attributes, listeners, setNodeRef, transform} = useDraggable({
6
id: 'draggable',
7
});
8
const style = transform ? {
9
transform: `translate3d(${transform.x}px, ${transform.y}px, 0)`,
10
} : undefined;
11
12
13
return (
14
<button ref={setNodeRef} style={style} {...listeners} {...attributes}>
15
{props.children}
16
</button>
17
);
18
}
Copied!
As you can see from the example above, it really only takes just a few lines to transform your existing components into draggable components.
Tips:
    For performance reasons, we recommend you use transform over other positional CSS properties to move the dragged element.
    You'll likely want to alter the z-index of your Draggable component to ensure it appears on top of other elements.
    If your item needs to move from one container to another, we recommend you use the <DragOverlay> component so the item isn't constrained to its parent's stacking context and overflow constraints.
Converting the transform object to a string can feel tedious. Fear not, you can avoid having to do this by hand by importing the CSS utility from the @dnd-kit/utilities package:
1
import {CSS} from '@dnd-kit/utilities';
2
3
// Within your component that receives `transform` from `useDraggable`:
4
const style = {
5
transform: CSS.Translate.toString(transform),
6
}
Copied!

Assembling all the pieces

Once you've set up your Droppable and Draggable components, you'll want to come back to where you set up your <DndContext> component so you can add event listeners to be able to respond to the different events that are fired.
In this example, we'll assume you want to move your <Draggable> component from outside into your <Droppable> component:
To do so, you'll want to listen to the onDragEnd event of the <DndContext> to see if your draggable item was dropped over your droppable:
App.jsx
Droppable.jsx
Draggable.jsx
1
import React from 'react';
2
import {DndContext} from '@dnd-kit/core';
3
4
import {Droppable} from './Droppable';
5
import {Draggable} from './Draggable';
6
7
function App() {
8
const [isDropped, setIsDropped] = useState(false);
9
const draggableMarkup = (
10
<Draggable>Drag me</Draggable>
11
);
12
13
return (
14
<DndContext onDragEnd={handleDragEnd}>
15
{!isDropped ? draggableMarkup : null}
16
<Droppable>
17
{isDropped ? draggableMarkup : 'Drop here'}
18
</Droppable>
19
</DndContext>
20
);
21
22
function handleDragEnd(event) {
23
if (event.over && event.over.id === 'droppable') {
24
setIsDropped(true);
25
}
26
}
27
}
Copied!
1
import React from 'react';
2
import {useDroppable} from '@dnd-kit/core';
3
4
export function Droppable(props) {
5
const {isOver, setNodeRef} = useDroppable({
6
id: 'droppable',
7
});
8
const style = {
9
color: isOver ? 'green' : undefined,
10
};
11
12
13
return (
14
<div ref={setNodeRef} style={style}>
15
{props.children}
16
</div>
17
);
18
}
Copied!
1
import React from 'react';
2
import {useDraggable} from '@dnd-kit/core';
3
4
export function Draggable(props) {
5
const {attributes, listeners, setNodeRef, transform} = useDraggable({
6
id: 'draggable',
7
});
8
const style = transform ? {
9
transform: `translate3d(${transform.x}px, ${transform.y}px, 0)`,
10
} : undefined;
11
12
13
return (
14
<button ref={setNodeRef} style={style} {...listeners} {...attributes}>
15
{props.children}
16
</button>
17
);
18
}
Copied!
That's it! You've set up your first Droppable and Draggable components.

Pushing things a bit further

The example we've set up above is a bit simplistic. In the real world, you may have multiple droppable containers, and you may also want to be able to drag your items back out of the droppable containers once they've been dragged within them.
Here's a slightly more complex example that contains multiple Droppable containers:
App.jsx
Droppable.jsx
Draggable.jsx
1
import React from 'react';
2
import {DndContext} from '@dnd-kit/core';
3
4
import {Droppable} from './Droppable';
5
import {Draggable} from './Draggable';
6
7
function App() {
8
const containers = ['A', 'B', 'C'];
9
const [parent, setParent] = useState(null);
10
const draggableMarkup = (
11
<Draggable id="draggable">Drag me</Draggable>
12
);
13
14
return (
15
<DndContext onDragEnd={handleDragEnd}>
16
{parent === null ? draggableMarkup : null}
17
18
{containers.map((id) => (
19
// We updated the Droppable component so it would accept an `id`
20
// prop and pass it to `useDroppable`
21
<Droppable key={id} id={id}>
22
{parent === id ? draggableMarkup : 'Drop here'}
23
</Droppable>
24
))}
25
</DndContext>
26
);
27
28
function handleDragEnd(event) {
29
const {over} = event;
30
31
// If the item is dropped over a container, set it as the parent
32
// otherwise reset the parent to `null`
33
setParent(over ? over.id : null);
34
}
35
};
Copied!
1
import React from 'react';
2
import {useDroppable} from '@dnd-kit/core';
3
4
export function Droppable(props) {
5
const {isOver, setNodeRef} = useDroppable({
6
id: props.id,
7
});
8
const style = {
9
color: isOver ? 'green' : undefined,
10
};
11
12
13
return (
14
<div ref={setNodeRef} style={style}>
15
{props.children}
16
</div>
17
);
18
}
Copied!
1
import React from 'react';
2
import {useDraggable} from '@dnd-kit/core';
3
4
export function Draggable(props) {
5
const {attributes, listeners, setNodeRef, transform} = useDraggable({
6
id: props.id,
7
});
8
const style = transform ? {
9
transform: `translate3d(${transform.x}px, ${transform.y}px, 0)`,
10
} : undefined;
11
12
13
return (
14
<button ref={setNodeRef} style={style} {...listeners} {...attributes}>
15
{props.children}
16
</button>
17
);
18
}
Copied!
We hope this quick start guide has given you a glimpse of the simplicity and power of dnd kit. There's much more to learn, and we encourage you to keep reading about all of the different options you can pass to <DndContext> , useDroppable and useDraggable by reading their respective API documentation.
Last modified 7mo ago