question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Connecting monitor.getClientOffset to a DropTarget does not refresh props

See original GitHub issue

I created a DropTarget and connected getClientOffset as follows:

targetCollect = function (connect, monitor) {
  return {
    connectDropTarget: connect.dropTarget(),
    isOver: monitor.isOver(),
    clientOffset: monitor.getClientOffset()
  }
}

However, when moving drag within the component, the target does not receive updated props with each wiggle of the mouse (only on enter and exit). The target spec’s hover is called however repeatedly. Is there a reason that the clientOffset prop is not injected each time it is changed?

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Reactions:14
  • Comments:22 (3 by maintainers)

github_iconTop GitHub Comments

5reactions
noah79commented, May 4, 2016

+1. Running into this and wasting a lot of time trying to understand why getClientOffset() works fine in canDrop() - which does get called repeatedly… - but no way to forward to my control ‘where’ over the item we are. I need to be able to insert above and below a treenode, not just “on” it. canDrop() has no way that I can find that lets me actually tell my component which of the many “types” of drops the node has so that my control can redisplay accordingly. Changing the prop in canDrop resulted in an error. Subscribing to onMouseMove doesn’t work during drag and drop operations. This has been a huge waste of time so far…any help would be appreciated.

@ibash, would you mind posting a gist of what you did to solve your problem?

2reactions
KnutHellandcommented, Jul 29, 2016

Btw. one other really ugly hack around this issue can be to send in the coordinates as state from dropTarget.hover():

const dropTarget = {
    hover(props, monitor, component) {
        // HACK! Since there is an open bug in react-dnd, making it impossible
        // to get the current client offset through the collect function as the
        // user moves the mouse, we do this awful hack and set the state (!!)
        // on the component from here outside the component.
        component.setState({
            currentDragOffset: monitor.getClientOffset(),
        });
    },
    drop() { /* ... */ },
};

Of course there are tons of more correct ways to do this that would avoid abusing of setState like that. But at least this little hack is very condensed and may do the job until this issue is eventually fixed. It lets you hack around the bug without changing other components and/or files and without relying on library internals.

Edit: this is basically the same as noah79 does, I just didn’t read his code before now.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Next steps with React Drag and Drop -- Adding a Drop Target
In this post, we are going to talk about handling drop events to make sure that ... const workDropTarget = { hover: function(props,...
Read more >
ReactDnD sorting elements not updating on Dragging
DropTarget ; var Card = React.createClass({ render: function() { var connectDragSource = this.props.
Read more >
gaearon/react-dnd - Gitter
So basically what I think is happening is the high order components are not forwarding the default props to the Child Component rendered...
Read more >
[Solved]-React DnD showing entire list when dragging-Reactjs
What you should do is that you should have unique ids of each single item ... ITEM, { hover: (props, monitor, component) =>...
Read more >
Sortable Targets with React DnD
Just render the Containers on the screen and give them some props. What props? Well, definitely the Container should know which cards it...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found