当我使用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);
}
Run Code Online (Sandbox Code Playgroud)
有没有办法让它只触发一次?
在我的 React JS 应用程序中,我进行了许多 API 调用,
而不必指定: const BASE_URL = ' https://apiurlgoeshere.com/ '
在每个页面上,我宁愿在整个应用程序中都可以访问 BASE_URL,因此我可以只执行 BASE_URL + API_CALL 例如