如何推送,更新React功能组件对象中的数组内的元素

时间:2020-09-17 06:37:07

标签: reactjs react-hooks react-functional-component react-state-management

const [Collect, setCollect] = React.useState({
visible: false,
files:[],
uploading: 0,
team:"",
manager:"",
owner:"",
fileNames: [],
status:""

});

function fileOnChange(e){
// here update files and filenames arrays
}
我有一个功能组件,该组件的对象带有上述数组。每当从输入中选择新文件时,如何更新数组? fileOnChange方法来更新添加文件和文件名

1 个答案:

答案 0 :(得分:0)

您可以创建另一个变量来存储以前的文件名和文件

let temp1 = Collect.filenames;
let temp2 = Collect.files;
temp1.push('anotherFilename');
temp2.push('anotherFile');
setCollect({...Collect, filenames: temp1, files: temp2});