React.js - setDragImage on a draggable item

I am trying to change the default image used by the browser when I drag and drop the item being dragged (in this case ulbelow). Here is my code. I am not getting any errors - it just does not work, and I still have the default image used by the browser.

drag(e) {
  let img = new Image()
  img.src = 'https://some-image.png'
  e.dataTransfer.setDragImage(img, 0, 0)
}

render() {
  return(
    <ul draggable="true" onDrag={(e) => {this.drag(e)}>
      <li>1</li>
      <li>2</li>
    </ul>
  )
}
+4
source share
1 answer

Image is not uploaded when you call setDragImage(). I processed this by creating an image on a mount and keeping it in a state:

 componentDidMount() {
    const img = new Image();
    img.src = 'https://some-image.png';
    img.onload = () => this.setState({ dragImg: img });
 }
 drag(e) {
    e.dataTransfer.setDragImage(this.state.dragImg, 0, 0);
 }
+1
source

Source: https://habr.com/ru/post/1681248/


All Articles