如何在功能组件中实现react-dnd-text-dragpreveiew?

发布于 2025-01-21 20:44:37 字数 1523 浏览 1 评论 0 原文

我正在使用react-dnd,并且可以在拖动时放置图像,但是现在我想代替想要自定义文本的图像。

我发现此组件react-dnd-text-dragpreview,但示例是针对React类组件。

我试图将“ DragPreviewImage”放在“ DragPreviewImage”的SRC中,但行不通。

有人可以帮我吗?

提前致谢 !

< DragPreview

带有图像的示例代码

...
import { DragPreviewImage, useDrag } from 'react-dnd';
import { boxImage } from '../components/boxImage';
import { createDragPreview } from 'react-dnd-text-dragpreview'


function FieldDrag({ field, dropboxField, onDragEnd = () => null, setFieldValue = () => null, cargoCategories }) {
const [{ isDragging }, drag, preview] = useDrag(() => ({
    type: 'field',        
    item: { id: field.id, dragCargoInfoId: field.dragCargoInfoId, dragCargoInfo: field.childDragCargoInfo },
    collect: (monitor) => ({
        isDragging: !!monitor.isDragging(),
    }),
    end: (item) => endDrag(item),
}));

const endDrag = (item) => {
    onDragEnd(item);
};

const styles = {
  fontSize: '12px'
}

const dragPreviewImage = createDragPreview('Custom Drag Text', styles);

.....

 return (
  <>
    <DragPreviewImage connect={preview} src={boxImage} />
    <span ref={drag} className="flex-item" style={{ ...computedStyle, ...styleDropboxField }}>
        {getField(field, extraStyle, isboxed, cargoCategories)}
    </span>
  </>
);

拖动

i'm using react-dnd and i'm able to put an image when draging, but now i would like to instead of a image i want a custom text.

i found this component react-dnd-text-dragpreview, but the example is for react class component.

i've tried to put "dragPreviewImage" in the src of "DragPreviewImage" , but doesn't work.

can someone help me on this ?

thanks in advance !

https://www.npmjs.com/package/react-dnd-text-dragpreview

sample code

...
import { DragPreviewImage, useDrag } from 'react-dnd';
import { boxImage } from '../components/boxImage';
import { createDragPreview } from 'react-dnd-text-dragpreview'


function FieldDrag({ field, dropboxField, onDragEnd = () => null, setFieldValue = () => null, cargoCategories }) {
const [{ isDragging }, drag, preview] = useDrag(() => ({
    type: 'field',        
    item: { id: field.id, dragCargoInfoId: field.dragCargoInfoId, dragCargoInfo: field.childDragCargoInfo },
    collect: (monitor) => ({
        isDragging: !!monitor.isDragging(),
    }),
    end: (item) => endDrag(item),
}));

const endDrag = (item) => {
    onDragEnd(item);
};

const styles = {
  fontSize: '12px'
}

const dragPreviewImage = createDragPreview('Custom Drag Text', styles);

.....

 return (
  <>
    <DragPreviewImage connect={preview} src={boxImage} />
    <span ref={drag} className="flex-item" style={{ ...computedStyle, ...styleDropboxField }}>
        {getField(field, extraStyle, isboxed, cargoCategories)}
    </span>
  </>
);

drag with image
enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

妳是的陽光 2025-01-28 20:44:37

我在codesandbox中找到了解决方案 - &gt;

只需添加src image“ dragpreviewimage.src”即可。

<DragPreviewImage connect={preview} src={dragPreviewImage && 
   dragPreviewImage.src} />

I found the solution in the codesandbox --> https://codesandbox.io/s/uoplz?file=/src/module-list.jsx:2522-2619

just put src image "dragPreviewImage.src".

<DragPreviewImage connect={preview} src={dragPreviewImage && 
   dragPreviewImage.src} />
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文