question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

<Dropzone> cause onClick to fire twice on parent <div>

See original GitHub issue

I ran into an issue with click handlers and drag handlers being fired twice on a <div>, and found that it happens when I have <Dropzone> as a child of the <div>.

Before <Dropzone>:

<div onClick={e => console.log("click", e)}>
</div>

Clicking the <div> results in: image

After <Dropzone>:

<div onClick={e => console.log("click", e)}>
  <Dropzone className="upload-drop-area"
            activeClassName="upload-drop-area-accept"
            rejectClassName="upload-drop-area-reject"
            multiple={false}
            onDragEnter={this.onDragEnter}
            onDragLeave={this.onDragLeave}
            onDropAccepted={this.onDropAccepted}>
  </Dropzone>
</div>

Clicking the <div> results in two events: image

I’ve never seen this kind of thing in React before. Any idea what’s going on?

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:47

github_iconTop GitHub Comments

6reactions
LaustAxelsencommented, May 1, 2017

Just add a pointer-events: none; css-rule on the dropzone DIV dom-node… that fixed it for me.

3reactions
mweibelcommented, Nov 27, 2019

Maybe a few more pointers: I experienced the same issue as here but it only seems to happen in Chrome. Firefox handles it correctly (IMO). Code to paste on https://react-dropzone.js.org:

import React from 'react';
import {useDropzone} from 'react-dropzone';

function Basic(props) {
  const {acceptedFiles, getRootProps, getInputProps, open} = useDropzone();

  const files = acceptedFiles.map(file => (
    <li key={file.name}>
      {file.name} - {file.size} bytes
    </li>
  ));

  return (
    <section className="container">
      <div {...getRootProps()}>
        <input {...getInputProps()} />
        <p>Drag 'n' drop some files here, or click to select files</p>
        <button onClick={open}>Browse Files</button>
      </div>
      <aside>
        <h4>Files</h4>
        <ul>{files}</ul>
      </aside>
    </section>
  );
}

<Basic />

Reproducing:

  1. click on the dropzone: all works well. File dialog only opens once
  2. click on browse files button: FF: all works well. Chrome: file dialog opens twice, and only the second one actually uploads the file

Using e.preventDefault() doesn’t help:

<button onClick={(e) => { e.preventDefault(); open(e)}>Browse Files</button>

only removing the onClick handler seems to help and all still works.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Dropzone opens file chooser twice - Stack Overflow
This bug happens when your clickable element is already an input[type=file]. Dropzone injects another one and now you have two.
Read more >
react-dropzone
Simple React hook to create a HTML5-compliant drag'n'drop zone for files. Documentation and examples at https://react-dropzone.js.org.
Read more >
Event Bubbling and Event Catching in JavaScript and React
She has a single parent div with an onClick event handler that, when clicked, calls everyone to the table to eat her food....
Read more >
How to correctly use preventDefault(), stopPropagation(), or ...
A fileUpload function to trigger the click event on the file upload input . Assigning both the dropzone div and a button to...
Read more >
Introduction to browser events - The Modern JavaScript Tutorial
click – when the mouse clicks on an element (touchscreen devices ... and use double quotes inside, like this: onclick="alert("Click!
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found