react-dnd 主要是依赖拖拽传输数据,构建新的dom,不建议拖拽原dom。
拖拽目标 dragTarget:
import React from 'react';
import { useDrag } from 'react-dnd';
const DragTarget = (props) => {
const [collected, dragRef] = useDrag(() => ({
type: 'xxx',
item: { id },
canDrag: (monitor) => {
},
}), [
return (
<div ref={dragRef}>
</div>
);
};
export default DragTarget;
放置目标 dropTarget:
import React,{ useRef } from 'react';
import { useDrop } from 'react-dnd';
const DropTarget = () => {
const dropCon = useRef();
const [collectedProps, dropRef] = useDrop(() => ({
accept: 'xx',
drop: (item, monitor) => {
}
}));
return (
<div ref={dropRef(dropCon)}>
<iframe></iframe>
</div>
);
};
export default DropTarget;
一般元素 dragTarget都能正常放上去,但是iframe则不行,解决办法:
当鼠标放置在dragTarget上时,将iframe的pointer-events设置为none,放置成功后设置回来就行了。