【发布时间】:2019-05-28 08:25:26
【问题描述】:
我有以下功能,可将<input type="file" multiple /> 中的附件添加到父母的状态。
在用户想要添加另一个附件之前效果很好,新附件会覆盖现有附件,如何将新附件添加到现有附件数组中?
handleAddAttachments = fileList => {
this.props.handleChange('attachments', Array.from(fileList));
};
我试过了,但它似乎不起作用。
handleAddAttachments = fileList => {
const { attachments } = this.props.ticket;
this.props.handleChange('attachments', [...attachments, fileList]);
};
父handleChange函数:
makeHandleChange = (pageName, change) => {
this.setState({
ticket: { ...this.state.ticket, [pageName]: change },
});
};
【问题讨论】:
-
如果你
console.log([...attachments, fileList])看到预期的结果了吗? -
@sigmus,不,假设我先添加了 3 个附件,然后添加了第 4 个附件,我看到的是
[{"0": {},"1": {},"2": {}}{"0": {}}],而不是我希望看到的[{},{},{},{}]。
标签: javascript