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.

Updating state in hover of dragged component and hover component

See original GitHub issue

I have a DropTarget and DragSource for the same component and I need to adjust the state on hover of the component being hovered on and the component being dragged. Within hover() I need to update the state of the component being hovered on and the component being dragged.

I haven’t quite figured out a good way to do this. I can wrap this in another container component and pass props and update the state of the container being hovered on, but how do I update the state of the item being dragged at the same time?

Thank you

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:7

github_iconTop GitHub Comments

3reactions
cliffmeyerscommented, Nov 29, 2017

I’m needing to do essentially the same thing here: I want to highlight the top or bottom border of the item in a vertical list as the user drags above and below its vertical midpoint, to indicate whether the dropped item would be placed before or after that item.

The Drop Target spec’s hover method is called repeatedly - which is fine - as I’ve added some logic to short-circuit execution unless my position value actually changes from “above” or “below” (and vice versa). Unfortunately I don’t have a clean mechanism to update the props for the component instance passed to hover. I can’t modify the props argument nor can I set component.props - and I think that either would be a bit of a kludge even if possible.

The only way I’ve been able to make this work is

  1. Pass a callback function as a prop down into my item component,
  2. Invoke the callback function on props inside the hover function on the target spec
  3. Update the state of my list so that the my list receives new props, such that the data object corresponding the hovered item has a flag to indicate whether above or below is active.

This isn’t really ideal since it pollutes my data model with display-only concerns I’d prefer not to mix in there.

Is there some way that hover could support a return value which are one or more props to be merged into the props for the specified component? Then I could more easily apply the styling with a component composed around my actual item component.

2reactions
evelantcommented, Jun 27, 2018

@cliffmeyers If you check out #1096 I found a somewhat more elegant (but still hack-ish) way to do it.

Read more comments on GitHub >

github_iconTop Results From Across the Web

During drag and drop if any component hover over the current ...
Just changing the value of a variable used in React won't force a rerender - just changing the value of newStyle won't do...
Read more >
Add multiple states to components - Adobe Support
In Design mode, click the + button next to the Main component's Default State in the Property Inspector, and select Hover State.
Read more >
Dragging Over An Element Without Triggering Hover State
This is an answer to a react-dnd v17 bug. It's still a bug even though it's marked as closed in Github.
Read more >
How to implement drag and drop in React with React DnD
At this point, you might be wondering why we need to update the state while hovering? Why not update it while dropping? It's...
Read more >
Next steps with React Drag and Drop -- Adding a Drop Target
const workDropTarget = { hover: function(props, monitor, component) { const item = monitor.getItem(); const draggedPosition = item.position; ...
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