小编Jam*_*ock的帖子

React JS - onChange 触发两次

当我使用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)

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

upload onchange reactjs

5
推荐指数
1
解决办法
4044
查看次数

如何在 React JS 中创建全局可访问的变量

在我的 React JS 应用程序中,我进行了许多 API 调用,

而不必指定: const BASE_URL = ' https://apiurlgoeshere.com/ '

在每个页面上,我宁愿在整个应用程序中都可以访问 BASE_URL,因此我可以只执行 BASE_URL + API_CALL 例如

variables global reactjs

5
推荐指数
1
解决办法
4152
查看次数

标签 统计

reactjs ×2

global ×1

onchange ×1

upload ×1

variables ×1