react-dnd icon indicating copy to clipboard operation
react-dnd copied to clipboard

Uncaught Error: Expected to find a valid target.

Open ghost opened this issue 8 years ago • 14 comments

Describe the bug Uncaught Error: Expected to find a valid target. Similar to https://github.com/react-dnd/react-dnd/issues/361, but in a different scenario.

I am using nested sources and targets. I have a list of items (each item being both a drag source and a drag target of type 'item'), with each item having a number of subitems (each subitem being both a drag source and a drag target of type 'subitem').

When swapping a dragged subitem with a hovered subitem within the same item, everything works as expected.

To Reproduce When swapping a dragged subitem with a hovered subitem of a different item, the following error occurs:

Uncaught Error: Expected to find a valid target. at invariant (browser.js:38) at DragDropMonitorImpl../node_modules/dnd-core/lib/DragDropMonitorImpl.js.DragDropMonitorImpl.canDropOnTarget (DragDropMonitorImpl.js:60) at HTML5Backend.js:428 at Array.some () at HTML5Backend../node_modules/react-dnd-html5-backend/lib/HTML5Backend.js.HTML5Backend.handleTopDragOver (HTML5Backend.js:427)

Expected behavior The swap occurs without the error.

Desktop:

ghost avatar Sep 01 '18 19:09 ghost

this happens for me as well +1

ad1992 avatar Sep 19 '18 11:09 ad1992

+1

delijah avatar Sep 24 '18 15:09 delijah

I can reproduce this on a setup like this:

@DropTarget(
	(props: DroppableProps) => props.accepts,
	{},
	(connect: DropTargetConnector, monitor: DropTargetMonitor) => ({
		connectDropTarget: connect.dropTarget()
	})
)
class Dustbin extends React.Component<DroppableProps> {
	public render() {
		return this.props.connectDropTarget && this.props.connectDropTarget(<div>{this.props.children}</div>);
	}
}

In my case the error occurs in the following line:

//	(props: DroppableProps) => props.accepts,

When I changed the code to anything but a function the error went away.

NO ERRORS

@DropTarget(
	["list", "of", "accepted", "types"],
	{},
	(connect: DropTargetConnector, monitor: DropTargetMonitor) => ({
		connectDropTarget: connect.dropTarget()
	})
)

NO ERRORS

@DropTarget(
	"my accepted type",
	{},
	(connect: DropTargetConnector, monitor: DropTargetMonitor) => ({
		connectDropTarget: connect.dropTarget()
	})
)

Sidenote:

The OP's error occurs while the hover method is active.

I get a different error when I perform the same action on drop

dragDrop.js:125 
Uncaught TypeError: Cannot read property 'drop' of undefined
    at dragDrop.js:125
    at Array.forEach (<anonymous>)
    at DragDropManagerImpl.drop (dragDrop.js:123)
    at Object.drop (DragDropManagerImpl.js:40)
    at HTML5Backend../node_modules/react-dnd-html5-backend/lib/HTML5Backend.js.HTML5Backend.handleTopDrop (HTML5Backend.js:477)
(anonymous) @ dragDrop.js:125
drop @ dragDrop.js:123
(anonymous) @ DragDropManagerImpl.js:40
./node_modules/react-dnd-html5-backend/lib/HTML5Backend.js.HTML5Backend.handleTopDrop @ HTML5Backend.js:477

pixelass avatar Oct 10 '18 23:10 pixelass

Started getting this error after upgrading a project from v2.5.4 to v6.0.0.

Everything seems to work fine on both v2.5.4 and v6.0.0 (other than v6.0.0 throwing an error in the console) so I'm not sure if there is no issue on v2.5.4 or if its just not being reported.

butchmarshall avatar Nov 14 '18 18:11 butchmarshall

+1

realb0t avatar Feb 20 '19 15:02 realb0t

+1

NZOleg avatar May 13 '19 21:05 NZOleg

The issue was resolved for me by making sure the target was still in the DOM. I had some conditional case that removed the target when the source was dragged into it causing this error to occur.

maciejzukowski avatar May 16 '19 15:05 maciejzukowski

+1, How do I avoid this error?

bexoss avatar Jun 10 '19 06:06 bexoss

make sure your the key of your item unrelated to the index to prevent the dom node being removed.

gaoupon avatar Jun 28 '19 11:06 gaoupon

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

stale[bot] avatar Aug 27 '19 12:08 stale[bot]

I added a debounce to the hover handler (with trailing option) and that solved the issue. The components were updating too quickly by setting the state before DnD could catch up, and the target ID had changed by the time the user dropped the item. So for those of you that could get the above solutions to work, maybe this may help.

GNARGNARDAVE avatar May 05 '21 14:05 GNARGNARDAVE

@GNARGNARDAVE can you please provide sample code for how to add a debounce to the hover handler?

benjamincassidy avatar Sep 27 '21 18:09 benjamincassidy

This is what I used and it worked for me. You should add a console output to observe the target id as you hover the item - and you'll notice that the target id changed/doesn't sync because everything is happening too quick; try adding it before and after to see the result.

onDragOver={handleDragOver}

let debouncedFn:any = null;

const handleDragOver = (e:any):void => {

        e.persist();
        e.preventDefault();

        if (!debouncedFn) {
            debouncedFn = debounce(()=> {
                e.dataTransfer.dropEffect = "move";
                const parentId = (e.target.parentNode) ? e.target.parentNode[keyId] : "";
                const targetId = e.target[keyId] || parentId; // items can be dropped on a child
                const hoverIndex = (targetId) ? state.data.findIndex((item:any) => item[keyId].toString() === targetId.toString()) : -1;

                if (hoverIndex !== state.hoverIndex) {
                    setState((prevState: IState<ITableData>) => ({
                        ...prevState,
                        hoverIndex,
                    }));
                }
            }, 100);
        }

        debouncedFn();
    };

GNARGNARDAVE avatar Sep 27 '21 19:09 GNARGNARDAVE

I have the same problem and fix it by this way。

//before 
moveItem(dragIndex, hoverIndex);
item.index = hoverIndex;
const [, drop] = useDrop({
      accept: ItemType,
      hover(item, monitor) {
        const dragIndex = item.index;
        const hoverIndex = index;

        if (dragIndex === hoverIndex) {
          return;
        }
        item.index = hoverIndex;
        setTimeout(() => {
          // 不可以同步进行, 这样会导致dnd报错, 要异步的去更新数组
          moveItem(dragIndex, hoverIndex);
        }, 0);
      }
    })

don`t believe in chat GPT ~ give code 1 second , debug half hours .

murongqimiao avatar Jun 25 '24 03:06 murongqimiao