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.
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.
Share Improve this question asked Apr 18, 2018 at 11:37 SoleilSoleil 7,2876 gold badges45 silver badges71 bronze badges 3 |2 Answers
Reset to default 18You can use onDragStart
:
onDragStart={this.preventDragHandler}
And the handler:
preventDragHandler = (e) => {
e.preventDefault();
}
For apps using HTML5, setting draggable={false}
in react should accomplish this as well. It will wind up setting the HTML5 draggable attribute to "false".
Another option would be to just use CSS and set pointer-events: none;
on the image.
draggable="false"
) works for images for whichonClick={}
is doing something, but for the others, it works when I do this directly, but when I click once, the image is kind of selected and then I can drag it for unknown reason. – Soleil Commented Apr 18, 2018 at 11:47