Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Removing dragged element on hover throws "Expected to find a valid target." #361

Closed
giladv opened this issue Jan 23, 2016 · 23 comments
Closed
Labels

Comments

@giladv
Copy link

giladv commented Jan 23, 2016

hi guys,

i cannot really post a specific code example as it's too cluttered at the moment but i think iv'e isolated the steps required to reproduce the issue.

  • start drag
  • on hover on dragged item (the item is both drop and drag connected)
    • remove state corresponding to dragged item

the error was thrown at DragDropMonitor.prototype.canDropOnTarget
because it couldn't find a target with the targetID (obviously).

use case: i"m building a sortable tree and i need to relocate the dragged & hovered item
to after it's parent node:

on more thing: while the error occurs, it still works.

thanks!

@rachelsison
Copy link

+1 I am also having this issue

@gaearon
Copy link
Member

gaearon commented Feb 13, 2016

Please provide an example reproducing the issue. I can take a look then.

@gaearon gaearon added the bug label Feb 13, 2016
@gaearon gaearon changed the title Uncaught Invariant Violation: Expected to find a valid target. - while removing dragged elem Removing dragged element on hover throws "Expected to find a valid target." Feb 13, 2016
@gaearon
Copy link
Member

gaearon commented Feb 14, 2016

This is probably fixed in 2.1.0.
I’ll close but please let me know if not, and I’ll reopen.

@ad1992
Copy link

ad1992 commented Sep 19, 2018

@gaearon I recently upgraded to react-dnd v5 and this issue is replicated again. Seems like it worked fine in 2.6.0 and also 3.0.2 but breaks in 4.0.5 and 5.0.0.

@mwielocha
Copy link

Any news on this?

@ad1992
Copy link

ad1992 commented Sep 24, 2018

@gaearon any update?

@Jarch09
Copy link

Jarch09 commented Nov 8, 2018

Same problem here

@drjkuo
Copy link

drjkuo commented Nov 19, 2018

same here in 5.0.0

@happier2
Copy link
Contributor

same problem here

@dmitrii-esin
Copy link

same problem here

image

@ravi5b2
Copy link

ravi5b2 commented Oct 23, 2019

same problem , Any suggestions for fix ?

@prateekkarkare
Copy link

Anyone had any luck with this?

@neilshah2000
Copy link

I have this issue on react-sortable-tree external source sample. Dragging to a red area.

https://frontend-collective.github.io/react-sortable-tree/?path=/story/advanced--drag-from-external-source
https://github.com/frontend-collective/react-sortable-tree/blob/master/stories/external-node.js

vendors~main.91acd83f50dfbcb80997.bundle.js:1 Uncaught Invariant Violation: Expected to find a valid target. at module.exports (https://frontend-collective.github.io/react-sortable-tree/vendors~main.91acd83f50dfbcb80997.bundle.js:2:592) at DragDropMonitorImpl.canDropOnTarget (https://frontend-collective.github.io/react-sortable-tree/vendors~main.91acd83f50dfbcb80997.bundle.js:2:186636) at DropTargetMonitorImpl.canDrop (https://frontend-collective.github.io/react-sortable-tree/vendors~main.91acd83f50dfbcb80997.bundle.js:2:71541) at https://frontend-collective.github.io/react-sortable-tree/main.91acd83f50dfbcb80997.bundle.js:1:132866 at DragDropContainer.getCurrentState (https://frontend-collective.github.io/react-sortable-tree/vendors~main.91acd83f50dfbcb80997.bundle.js:2:87049) at DragDropContainer._this.handleChange (https://frontend-collective.github.io/react-sortable-tree/vendors~main.91acd83f50dfbcb80997.bundle.js:2:84210) at handleChange (https://frontend-collective.github.io/react-sortable-tree/vendors~main.91acd83f50dfbcb80997.bundle.js:2:185746) at dispatch (https://frontend-collective.github.io/react-sortable-tree/vendors~main.91acd83f50dfbcb80997.bundle.js:2:168058) at Object.endDrag (https://frontend-collective.github.io/react-sortable-tree/vendors~main.91acd83f50dfbcb80997.bundle.js:2:199234) at HTML5Backend.handleTopDragEndCapture (https://frontend-collective.github.io/react-sortable-tree/vendors~main.91acd83f50dfbcb80997.bundle.js:2:152270)

@prateekkarkare
Copy link

The following fix worked for me

frontend-collective/react-sortable-tree@9a84113

@GNARGNARDAVE
Copy link

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.

@karlavv
Copy link

karlavv commented Aug 10, 2021

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.

Thank you! @GNARGNARDAVE That really helped me. I wrapped my dnd function in useCallback and debounce like this:

const moveColumnToDebounce = (dragIndex, hoverIndex) => {
const dragCol = visibleColumns[dragIndex];
let arr = visibleColumns.slice();
arr.splice(dragIndex, 1);
arr.splice(hoverIndex, 0, dragCol);
setColumnOrder(arr.map(c => c && c.id));
};
const moveColumn = useCallback(debounce(moveColumnToDebounce, 300), [visibleColumns], isEqual);

It finally works!

@GNARGNARDAVE
Copy link

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.

Thank you! @GNARGNARDAVE That really helped me. I wrapped my dnd function in useCallback and debounce like this:

const moveColumnToDebounce = (dragIndex, hoverIndex) => {
const dragCol = visibleColumns[dragIndex];
let arr = visibleColumns.slice();
arr.splice(dragIndex, 1);
arr.splice(hoverIndex, 0, dragCol);
setColumnOrder(arr.map(c => c && c.id));
};
const moveColumn = useCallback(debounce(moveColumnToDebounce, 300), [visibleColumns], isEqual);

It finally works!

Glad that you were able to get it to work!

@ChickendCode
Copy link

I have this issue on react-sortable-tree external source sample. Dragging to a red area.

https://frontend-collective.github.io/react-sortable-tree/?path=/story/advanced--drag-from-external-source
https://github.com/frontend-collective/react-sortable-tree/blob/master/stories/external-node.js

vendors~main.91acd83f50dfbcb80997.bundle.js:1 Uncaught Invariant Violation: Expected to find a valid target. at module.exports (https://frontend-collective.github.io/react-sortable-tree/vendors~main.91acd83f50dfbcb80997.bundle.js:2:592) at DragDropMonitorImpl.canDropOnTarget (https://frontend-collective.github.io/react-sortable-tree/vendors~main.91acd83f50dfbcb80997.bundle.js:2:186636) at DropTargetMonitorImpl.canDrop (https://frontend-collective.github.io/react-sortable-tree/vendors~main.91acd83f50dfbcb80997.bundle.js:2:71541) at https://frontend-collective.github.io/react-sortable-tree/main.91acd83f50dfbcb80997.bundle.js:1:132866 at DragDropContainer.getCurrentState (https://frontend-collective.github.io/react-sortable-tree/vendors~main.91acd83f50dfbcb80997.bundle.js:2:87049) at DragDropContainer._this.handleChange (https://frontend-collective.github.io/react-sortable-tree/vendors~main.91acd83f50dfbcb80997.bundle.js:2:84210) at handleChange (https://frontend-collective.github.io/react-sortable-tree/vendors~main.91acd83f50dfbcb80997.bundle.js:2:185746) at dispatch (https://frontend-collective.github.io/react-sortable-tree/vendors~main.91acd83f50dfbcb80997.bundle.js:2:168058) at Object.endDrag (https://frontend-collective.github.io/react-sortable-tree/vendors~main.91acd83f50dfbcb80997.bundle.js:2:199234) at HTML5Backend.handleTopDragEndCapture (https://frontend-collective.github.io/react-sortable-tree/vendors~main.91acd83f50dfbcb80997.bundle.js:2:152270)

Have you solved this problem yet?

@GNARGNARDAVE
Copy link

I posted a potential solution - if you haven't tried it yet, I'd suggest putting in a console.log on the hover handler if you haven't tried to debounce solution on the handler. From the console log - output the target ids and you'll notice that the target id doesn't match up because it's triggering too many changes. Try adding the debounce to the hover handler

@ShahStackOverflow
Copy link

let me know if anyone find solution to this problem

@Aromokeye
Copy link

Aromokeye commented Dec 23, 2021

If you are still facing this problem, check how you update your collection (array) state when the dragging happens. Better to use an immutability helpers such as immerjs or immutability-helper.

@YoannBuzenet
Copy link

YoannBuzenet commented Jan 9, 2022

Anyone wondering about the Hover handler, here is a working example for me with the debounce:

const [{ isOver, canDrop }, drop] = useDrop(
    () => ({
      accept: types.PEC,
      canDrop: () => true,
      drop: () => console.log("it did drop ! On PEC :", order),
      collect: (monitor) => ({
        isOver: !!monitor.isOver(),
        canDrop: !!monitor.canDrop(),
      }),
      hover: debounce(() => setOver(order), 300),
    }),
    []
  );

SetOver is a custom state passed from the parent to update the order of all the elements.

@alexya
Copy link

alexya commented Feb 22, 2022

I met the same error.

I have a tree structure, when I dragged a node from one node to another node (change the parent), this error will happen. But if I only dragged a node in the current list (don't change the parent, just reorder), it is okay.
In the hover function, I don't really change the data collection in real-time. I updated the data collection just when dropping the node, in drop function.

const MyTreeItem: React.FC<Props> = (props: Props) => {

/*
  import {debounce, throttle } from "throttle-debounce";
  rootNode and Node are passed in through the props of the component
*/

  const ref = useRef(null);
  const [, dragRef] = useDrag({
    type: DragType, // const DragType = "my-drag-and-drop";
    item: dragItem, // type DragItem {id, name, parentId}
    canDrag: true,
    collect: monitor => ({
      isDragging: monitor.isDragging()
    })
  });

  const [{ isOver }, dropRef] = useDrop({
    accept: DragType,
    collect: monitor => ({
      isOver: monitor.isOver(),
      canDrop: monitor.canDrop()
    }),
    drop: debounce(500, true, (item: DragItem, _monitor: DropTargetMonitor) => {
         // I am using react-redux & redux-saga, dipatch the action to move the node to the target
         dispatch?.(moveItemTo(item.id, node.id));
    }),
    hover: throttle(300, true, (_item: DragItem, monitor: DropTargetMonitor) => {
      if (ref && ref.current && monitor.canDrop()) {
        const target = ref.current as HTMLElement;
        const currentRect = target.getBoundingClientRect();
        const clientOffset = monitor.getClientOffset();

        if (currentRect && clientOffset) {
          const dropPosition = calcDropToPosition(currentRect, clientOffset);
          // according to the dragNode and hoverNode position to draw the image/effect layout, etc.
        }
      }
    }),
    canDrop: (_item: DragItem, _monitor) => {
      return true;
    }
  });

  const dragDropRef = dragRef(dropRef(ref));
  return <TreeItem itemRef={dragDropRef} {...otherProps} />;
}

Hi, @karlavv I meet this issue too. Where did u put the function moveColumn to? assign it to the hover in the useDrop?

any help will be much appreciated.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests