0

I am encountering a very strange error in my React application. I have this code:

<input
  accept="image/*"
  onChange={doUpload}
  type="file"
  name="fileUploader"
  id="photoUploader"
  ref={fileRef}
/>

but when I click the input to select a file, I don't get the file explorer at all. I also noticed that some of my form's onSumit did not fire until I added an onClick handler to the submit button.

2
  • what is the value in {fileRef}. Try with just ref="upload" and see if issue happens. Commented Aug 27, 2019 at 11:00
  • add this. before doUpload Commented Aug 27, 2019 at 11:37

3 Answers 3

12

Wow, found the bug. I had an event.stopPropagation() somewhere in a nested component.

Sign up to request clarification or add additional context in comments.

2 Comments

So there is event.preventDefault() or event.stopPropagation(). I believe you ment to say one of those.
nice catch @laszlo-horvath, I'VE UPDATED IT
-1

I had the same issue, dont know why it is was happening but I did in this way

const fn = useCallback((event) => {
    console.log("clicked");
    event.stopPropagation();
}, []);


const handleFileClick = (event) => {
    if (fileRef.current) {
        fileRef.current.addEventListener("click", fn);
        fileRef.current.click();
    }
};

Comments

-3

You have to add this keyword. I hope it is useful for you:

       <input
          accept="image/*"
          onChange={this.doUpload}
          type="file"
          name="fileUploader"
          id="photoUploader"
          ref={this.fileRef}
        />

1 Comment

I'm using functional a component and my ref is defined as const fileRef = useRef(null);.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.