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.

Use same element as target and source with hooks

See original GitHub issue

When I use decorators I could use the same element as target and source:

let Node = ({ connectDragSource, connectDropTarget }) => {
  return connectDragSource(connectDropTarget(<div/>));
};

Node = compose(
  DropTarget(...),
  DragSource(...),
);

Could I implement the same behavior with hooks?

Issue Analytics

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

github_iconTop GitHub Comments

23reactions
mib32commented, Feb 25, 2020

A nice working solution for custom component

import { useDrag, useDrop } from 'react-dnd'

function DraggableComponent(props) {
  /* ... */
 function attachRef(el) {
    drag(el)
    drop(el)
  }
  return <Grid ref={attachRef}>...</Grid>
}
3reactions
zouxuozcommented, Jul 18, 2019

Sorry, I got some misunderstanding 😃

Solution:

const Node = () => {
  const [dragCollectedProps, connectDragSource] = useDrag(...);
  const [dropCollectedProps, connectDropTarget] = useDrop(...);

  return connectDragSource(connectDropTarget(<div/>));
};

P.S I could send PR with added this to FAQ if needed

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to target a specific item to toggleClick on using React ...
I'm having a hard time figuring out how to target a specific menus index with React Hooks - currently onClick, it opens ALL...
Read more >
The Complete Guide to useRef() and Refs in React
In this post you'll learn how to use React.useRef() hook to create persisted mutable values (also known as references or refs), as well...
Read more >
Hooks FAQ - React
Hooks are a new addition in React 16.8. They let you use state and other React features without writing a class. This page...
Read more >
The last guide to the useEffect Hook you'll ever need
Don't be afraid to use multiple useEffect statements in your component. While useEffect is designed to handle only one concern, you'll sometimes ...
Read more >
The Guide to Learning React Hooks (Examples & Tutorials)
Now, let's see how to do the same thing with Hooks, using a functional ... Consumer> tags around the DOM elements that I...
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