javascript - 如何在 react-beautiful-dnd 中将 onDragStart 附加到 Draggable?

标签 javascript reactjs react-beautiful-dnd

docs它说:

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 be null when isDragDisabled 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,
|};
我要附上onDragStartDraggable这样只有在拖动特定的一组元素时才会触发它(而不是将其附加到 DragDropContext )。
使用我做的文档中的示例:
<Draggable draggableId="draggable-1" index={0}>
  {(provided, snapshot) => (
    <div
      ref={provided.innerRef}
      {...provided.draggableProps}
      {...provided.dragHandleProps}
      onDragStart={() => console.log('onDragStart')}
    >
      Drag me!
    </div>
  )}
</Draggable>
但是它似乎不起作用。任何想法如何使它工作?

最佳答案

它只能在 DragDropContext 中用于 Draggable 元素。

<DragDropContext
         onDragStart={()=>{alert('onDragStart')}}
         onDragEnd={()=>{alert('onDragEnd')}}>
</DragDropContext>

关于javascript - 如何在 react-beautiful-dnd 中将 onDragStart 附加到 Draggable?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59167128/

相关文章:

javascript - 如何访问应用程序内的 package.json 数据?

javascript - React-beautiful-dnd - DroppableProvided > 找不到占位符。 - 怎么修?

javascript - React beautiful dnd 与 Material UI 列表中的问题

javascript - 下面两组代码,一组有效,另一组无效。谁能帮我理解为什么

javascript - 有没有办法在覆盖元素上有选择地使用鼠标事件?

asp.net - 如何使用 ASP.Net Ajax $addHandler 将参数传递给被调用函数

reactjs - React Native AppLoading 无需 Expo

javascript - 为什么即使 Parent Props 没有更新,getDerivedStateFromProps 也会被再次调用?

reactjs - ClickAwayListener 组件不适用于 DragDropContext

javascript - 在浏览器中选择全字