gpt4 book ai didi

javascript - 如何在ReactJS中设置将两个函数添加到 'onchange's

转载 作者:行者123 更新时间:2023-12-01 01:28:08 26 4
gpt4 key购买 nike

我是 Reactjs 新手,仍在努力学习 React。我有一些与如何在一个 onChange 事件中调用两个函数有关的问题,我尝试了很多,但不知道如何实现。请你帮助我好吗。谢谢您

函数1

handleChange = ({ fileList }) => this.setState({ fileList });

函数2

handleUpload = e => {
const reader = new FileReader();
const storeUser = JSON.parse(localStorage.getItem('user'));
reader.onload = function(upload) {
fetch(`http://..../api/s3/uploadtoaws`, {
headers: {
Accept: 'application/json',
'Content-Type': 'application/json',
},
method: 'POST',
body: JSON.stringify({
userId: storeUser._id,
type: 'employee',
content: upload.target.result,
key: e.file.name,
oldKey: '',
}),
})
.then(response => response.json())
.then(res => {
console.warn(res);
});
// .done();
};

reader.readAsDataURL(e.file.originFileObj);
};

事件实现

    <Upload
listType="picture-card"
fileList={fileList}
onPreview={this.handlePreview}
onChange={this.handleChange}
>

最佳答案

只需使用指定的参数调用 JSX 中的函数,但是将 handleUpload 函数用作柯里化(Currying)函数非常重要,这样您就可以访问 event文件列表

onChange={this.handleUpload(someObj)}

handleUpload = ({ fileList }) => (e) => { // fileList is a field in someObj
this.setState({ fileList });

const reader = new FileReader();
const storeUser = JSON.parse(localStorage.getItem('user'));
reader.onload = function(upload) {
fetch(`http://..../api/s3/uploadtoaws`, {
headers: {
Accept: 'application/json',
'Content-Type': 'application/json',
},
method: 'POST',
body: JSON.stringify({
userId: storeUser._id,
type: 'employee',
content: upload.target.result,
key: e.file.name,
oldKey: '',
}),
})
.then(response => response.json())
.then(res => {
console.warn(res);
});
// .done();
};

reader.readAsDataURL(e.file.originFileObj);
}

关于javascript - 如何在ReactJS中设置将两个函数添加到 'onchange's,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53564785/

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