import React, { useState } from 'react'; import {useDroppable, useDraggable} from '@dnd-kit/core'; import {DndContext} from '@dnd-kit/core'; function Draggable(props) { const {attributes, listeners, setNodeRef, transform} = useDraggable({ id: props.id, }); const style = transform ? { transform: `translate3d(${transform.x}px, ${transform.y}px, 0)`, } : undefined; return ( ); } function Droppable(props) { const {isOver, setNodeRef} = useDroppable({ id: props.id, }); const style = { color: isOver ? 'green' : undefined, }; return (
{props.children}
); } export default function App() { const containers = ['A', 'B', 'C']; const [parent, setParent] = useState(null); const draggableMarkup = ( Drag me ); return ( {parent === null ? draggableMarkup : null} {containers.map((id) => ( // We updated the Droppable component so it would accept an `id` // prop and pass it to `useDroppable` {parent === id ? draggableMarkup : 'Drop here'} ))} ); function handleDragEnd(event) { const {over} = event; // If the item is dropped over a container, set it as the parent // otherwise reset the parent to `null` setParent(over ? over.id : null); } };