gpt4 book ai didi

reactjs - React JS - onChange 触发两次

转载 作者:行者123 更新时间:2023-12-03 23:14:38 24 4
gpt4 key购买 nike

当我使用 react-image-uploader 上传图像时,onchange 会触发两次。所以它尝试将图像上传到后端两次,这是我的处理方式:

//user uploads image to app
<ImageUploader
buttonClassName={"btn add-btn bg-orange"}
buttonText='ADD'
onChange={this.newProfilePicture}
imgExtension={['.jpg', '.gif', '.png', '.gif']}
maxFileSize={5242880}
fileSizeError="file size is too big"
fileTypeError="this file type is not supported"
singleImage={true}
withPreview={true}
label={""}
withIcon={false}
/>



//image is set to this.userprofilepicture
newProfilePicture = (picture) => {
this.setState({ userprofilepicture: picture});
this.setNewProfilePicture();
ShowAll();
}

//new profilepicture is uploaded to api
setNewProfilePicture = () => {
let data = new FormData();
console.log('profile picture: ', this.state.userprofilepicture)
data.append('Key', 'profilePicture');
data.append('Value', this.state.userprofilepicture)
this.sendUpdatedPicture('uploadprofilepicture', data);
}

有没有办法让它只触发一次?

最佳答案

如果您正在使用 create-react-app那么你的 App组件被包裹在 StrictMode 中像这样:

<React.StrictMode>
<App />
</React.StrictMode>,
转至 index.js 并删除 <React.StrictMode></React.StrictMode> https://github.com/facebook/react/issues/12856#issuecomment-390206425

关于reactjs - React JS - onChange 触发两次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53480107/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com