Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

onDrop event not fired in react app

I want to drag from a image to a span like this:

DRAG FROM HERE

<img src={img_src} draggable='true' onDragStart={this.dragStartHandler(idx)}/>

DROP HERE:

<span onDragOver={this.preventDefault} onDrop={this.dropHandler} 
    onClick={this.handleClick}>DROP HERE</span>

DRAG AND DROP HANDLERS:

  dragStartHandler = (idx) => (event) => {
    console.log(idx,"called drag event and loaded data in drag object")
    event.dataTransfer.setData('text', idx); 
  }

  dropHandler = () => (event) => {
    event.preventDefault();
    console.log("DROP EVENT")
  }

  preventDefault = () => (event) => {
    event.preventDefault();
    console.log("prevent default")
  }

A fiddle with this code is here: https://jsfiddle.net/69z2wepo/229855/

But the drop event is never called when i click on the draggable image, drag it over the drop area and release the mouse button. This is on chrome, firefox and safari.

How can I call the drop event correctly?

like image 691
user2212461 Avatar asked Nov 19 '25 20:11

user2212461


1 Answers

Solution:

Change

onDragOver={this.preventDefault}

to

onDragOver={this.preventDefault()}

if you're on ES6. Never harmful to go back to the basics :-)

like image 110
user2212461 Avatar answered Nov 22 '25 09:11

user2212461



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!