apps/docs/react/hooks/use-droppable.mdx
import {CodeSandbox} from '/snippets/sandbox.mdx'; import {draggableStyles, droppableStyles} from '/snippets/code.mdx';
<CodeSandbox files={{ 'App.js': {code: app, hidden: true}, 'Droppable.js': {code: droppableCode, active: true}, 'styles.css': {code: draggableStyles + '\n\n' + droppableStyles + '\n\n' + containerStyles, hidden: true}, }} height={255} previewHeight={400} hero />
The useDroppable hook requires an id and accepts all the same options as the Droppable class. Refer to the Input section below for more information.
import {useDroppable} from '@dnd-kit/react';
function Droppable(props) {
const {isDropTarget, ref} = useDroppable({
id: props.id,
});
return (
<div ref={ref}>
{isDropTarget ? 'Draggable element is over me' : 'Drag something over me'}
</div>
);
}
The useDroppable hook accepts the following arguments:
The useDroppable hook returns an object containing the following properties:
export const app = ` import {useState} from 'react'; import {DragDropProvider, useDraggable} from '@dnd-kit/react'; import {Droppable} from './Droppable'; import './styles.css';
function Draggable() { const {ref} = useDraggable({id: 'draggable'}); return <button ref={ref} className="btn">draggable</button>; }
export default function App() { const [parent, setParent] = useState(undefined); return ( <DragDropProvider onDragEnd={(event) => { if (event.canceled) return; setParent(event.operation.target?.id); }} > <div className="container"> {parent == null ? <Draggable /> : null} <Droppable> {parent === 'droppable' ? <Draggable /> : null} </Droppable> </div> </DragDropProvider> ); } `.trim();
export const droppableCode = ` import {useDroppable} from '@dnd-kit/react';
export function Droppable({children}) { const {isDropTarget, ref} = useDroppable({id: 'droppable'});
return ( <div ref={ref} className={isDropTarget ? "droppable active" : "droppable"}> {children} </div> ); } `.trim();
export const containerStyles = .container { display: grid; grid-template-columns: 2fr 1fr; grid-gap: 20px; align-items: center; max-width: 700px; margin: 0 auto; }.trim();