Skip to content

In useDrag callback function, how to get access to the element which triggered the useDrag #562

Answered by dbismut
Just-Moh-it asked this question in Q&A
Discussion options

You must be logged in to vote

The element that triggered useDrag is event.currentTarget.
You definitely shouldn't assign a ref to several elements, prototyping or not prototyping.

You should either do this:

useDrag(({ arg: [side] }) => {
  switch(side) {
    case 'top-left':
    case 'top-right':
    ...
  }
})

<div {...bind('top-left')} />
<div {...bind('top-right')} />

or

const Handle = ({ side }) => {
  const ref = useRef()
  useDrag(() => {
    switch(side) {
      case 'top-left':
      case 'top-right':
      ...
    }
  }, { target: ref })
}

  return <div ref={ref} />
}

const App = () => {
  return <>
    <Handle side="top-left" />
    <Handle side="top-right" />
  </>
}

Replies: 3 comments

Comment options

You must be logged in to vote
0 replies
Answer selected by Just-Moh-it
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants