【问题标题】:How do you move a dragsource with React-DND?你如何使用 React-DND 移动拖动源?
【发布时间】:2017-03-20 06:11:07
【问题描述】:

如何使用react-dnd 使源元素随鼠标光标移动?

这就是我正在做的事情(简化示例):

import React from 'react';
import ReactDom from 'react-dom';
import {DragDropContext, DragSource} from 'react-dnd';
import HTML5Backend from 'react-dnd-html5-backend';

@DragSource("zzzz", {beginDrag: props => ({})}, (connect, monitor) => ({
  connectDragSource: connect.dragSource(),
  isDragging: monitor.isDragging()
}))
export class DND extends React.Component {
  render () {
    const text = this.props.isDragging ? "YAY": "DND";

    return this.props.connectDragSource(
      <div>{text}</div>
    );
  }
}

@DragDropContext(HTML5Backend)
export class Application extends React.Component {
  render() {
    return <div><DND /></div>;
  }
}

ReactDom.render(
  <Application />,
  document.getElementById('react-mount')
);

当我单击并拖动它时,文本从DND变为YAY,但div没有跟随鼠标移动。

我所经历的示例似乎没有手动更新目标 x/y 坐标,我假设这是由库本身管理的。我错过了什么?

编辑:

我没有提到在 Chrome 开发工具中模拟移动设备时会发生这种情况。它在桌面模式下按预期工作。

我目前正在考虑使用DragLayer 移动拖动源,如here 所示;

【问题讨论】:

    标签: javascript reactjs drag-and-drop react-dnd


    【解决方案1】:

    移动浏览器在拖动时不显示拖动源的阴影。您需要使用DragPreview 来绘制移动。

    【讨论】:

    • 我一直在 Chrome 开发工具中模拟移动平台。看起来幽灵在桌面模式下创建的。我会去学习DragPreview的工作原理,谢谢。
    猜你喜欢
    • 1970-01-01
    • 2023-01-26
    • 1970-01-01
    • 2016-02-16
    • 2020-05-05
    • 1970-01-01
    • 2021-04-15
    • 1970-01-01
    • 2020-07-12
    相关资源
    最近更新 更多