简体   繁体   English

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

[英]how to push to, update elements inside an array that is in a object of a react functional component

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

}); });

function fileOnChange(e){
// here update files and filenames arrays
}

i have a functional component that has an object with arrays as shown above.我有一个功能组件,它有一个带有数组的对象,如上所示。 how can u update the arrays everytime new files selected from the input.每次从输入中选择新文件时,您如何更新数组。 the fileOnChange method to update add files and filesnames用于更新添加文件和文件名的 fileOnChange 方法

You can create another variable to store previous filenames and files您可以创建另一个变量来存储以前的文件名和文件

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


声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM