How to attach onDragStart to Draggable in react-beautiful-dnd?
In the docs it says:
DragHandleProps
need to be applied to the node that you want to be the drag handle. This is a number of props that need to be applied to the<Draggable />
node. The simplest approach is to spread the props onto the draggable node ({...provided.dragHandleProps}
). However, you are also welcome to monkey patch these props if you also need to respond to them.DragHandleProps
will benull
whenisDragDisabled
is set to true.
dragHandleProps
Type information
type DragHandleProps = {|
// what draggable the handle belongs to
'data-rbd-drag-handle-draggable-id': DraggableId,
// What DragDropContext the drag handle is in
'data-rbd-drag-handle-context-id': ContextId,
// Id of hidden element that contains the lift instruction (nicer screen reader text)
'aria-labelledby': ElementId,
// Allow tabbing to this element
tabIndex: number,
// Stop html5 drag and drop
draggable: boolean,
onDragStart: (event: DragEvent) => void,
|};
I want to attach onDragStart
to the Draggable
so that it is fired only when a particular set of elements is dragged (as opposed to attaching it to DragDropContext
).
Using the example from the docs I do:
<Draggable draggableId="draggable-1" index={0}>
{(provided, snapshot) => (
<div
ref={provided.innerRef}
{...provided.draggableProps}
{...provided.dragHandleProps}
onDragStart={() => console.log('onDragStart')}
>
Drag me!
</div>
)}
</Draggable>
however it doesn't seem to be working. Any ideas how to make it work?
It can only be used only in DragDropContext for the Draggable element.
<DragDropContext
onDragStart={()=>{alert('onDragStart')}}
onDragEnd={()=>{alert('onDragEnd')}}>
</DragDropContext>
onDragStart
and onDragEnd
are events for DragDropContext
component only.