webpack 如何在React中防止文件上传时重新加载浏览器?

rjee0c15  于 5个月前  发布在  Webpack
关注(0)|答案(2)|浏览(61)

我有一个简单的文件上传形式。

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;

字符串
问题是,当我在移动的设备上上传文件时,我的应用程序会刷新,整个应用程序会重新挂载。
然而,它只发生在部署上,并且在本地主机上工作良好。所以也许问题可能在WebPack配置中?
我试图修复它使用下面的代码在根组件,但它没有帮助.

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

ecr0jaav

ecr0jaav1#

我看不到你的表单提交函数,但也许你可以尝试添加event.preventDefault()event.stopPropagation()到它,或者在这种情况下添加到你的onFileUpload函数

fumotvh3

fumotvh32#

我们只是在应用程序的某些遗留部分中的visibilitychange事件侦听器中有!IS_LOCAL && window.location.reload();,这触发了此行为。
如果有人会面临同样的问题,留下这条评论。尝试在你的代码库中搜索window.location.reload();

相关问题