skip to Main Content

I have simple file upload form.

import React, { useState } from 'react';
import { FlexContainer } from '@styles/FlexContainer';

const TestUpload = () => {
  const [file, setFile] = useState<File>();

  const onFileUpload = (event: React.ChangeEvent<HTMLInputElement>) => {
    if (!event.target.files) return;

    setFile(event.target.files[0]);
  };

  return (
    <FlexContainer>
      <input
        type="file"
        onChange={onFileUpload}
        accept="image/*"
        capture="camera"
        id="fileFromCamera"
      />
      <input
        type="file"
        onChange={onFileUpload}
        accept="image/*"
        id="fileWithoutCamera"
      />
    </FlexContainer>
  );
};

export default TestUpload;

The problem is that when I’m uploading files on mobile devices, my app refreshes and the whole app remounts.

However, it happens only on deploy and works good on localhost. So maybe the issue could be in WebPack config?

I tried to fix it using code below in root component, but it didn’t help.

  useEffect(() => {
    const handleBeforeUnload = (event: BeforeUnloadEvent) => {
      event.preventDefault();
    };
    window.addEventListener('beforeunload', handleBeforeUnload);
    return () => {
      window.removeEventListener('beforeunload', handleBeforeUnload);
    };
  }, []);

2

Answers


  1. Chosen as BEST ANSWER

    We just had !IS_LOCAL && window.location.reload(); in visibilitychange event listener in some legacy parts of app, which triggered this behaviour.

    In case someone will face same issue, leaving this comment. Try to search window.location.reload(); in your codebase.


  2. I can’t see your form submit function but perhaps
    you might try adding event.preventDefault() and event.stopPropagation()
    to it or in this case to your onFileUpload function

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search