Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I prevent drag and drop images in a react app?

We got how to prevent drag drop image with jquery with $('img').on('dragstart', function(event) { event.preventDefault(); }); but I want to achieve the same in a react app, and without jquery. How can I do this ? a) locally (image per image) b) globally (for all images of the react app)

I still want to keep control over other interactions, so img { pointer-events: none; } is not a solution.

The goal is to prevent saving image through drag and drop.

like image 418
Soleil Avatar asked Apr 18 '18 11:04

Soleil


1 Answers

You can use onDragStart:

onDragStart={this.preventDragHandler}

And the handler:

preventDragHandler = (e) => {
  e.preventDefault();
}
like image 151
Bhojendra Rauniyar Avatar answered Oct 18 '22 22:10

Bhojendra Rauniyar